python - pandas 按天、周或月分组以获取时间戳

标签 python pandas

我是 Pandas 初学者。

我有以下数据:

a = [{"content": '1', "time": 1577870427}, 
     {"content": '4', "time": 1577870427},
     {"content": '2', "time": 1577956827},
     {"content": '3', "time": 1580548827}, 
     {"content": '4', "time": 1580635227},
     {"content": '5', "time": 1583054427}, 
     {"content": '6', "time": 1583140827}]

我想要:

2020-01: [
     {"content": '1', "time": '2020-01-01'},
     {"content": '4', "time": '2020-01-01'},
     {"content": '2', "time": '2020-01-02'},
    ]

    2020-02: [
     {"content": '3', "time": '2020-02-01'}, 
     {"content": '4', "time": '2020-02-02'},
    ]

    2020-03: [
     {"content": '5', "time": '2020-03-01'}, 
     {"content": '6', "time": '2020-03-02'}
    ]

最佳答案

您可以通过to_datetime将列时间转换为日期时间带有 unit 参数并用于自定义格式使用 Series.dt.strftime :

df = pd.DataFrame(a)
d = pd.to_datetime(df['time'], unit='s')
df['time'] = d.dt.strftime('%Y-%m-%d')
g = d.dt.strftime('%Y-%m')

d1 = {k: v.to_dict('r') for k, v in df.groupby(g)}

关于python - pandas 按天、周或月分组以获取时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60316730/

相关文章:

python - 如何在 Flask 中获取自定义 SELECT 查询?

python - RabbitMQ 和 Python - 连接无法关闭

python - 将系列设置为索引

python - 拆分 pandas 数据框行时出现问题?

Python 读取带有希伯来文标题的 csv

python - 对 pandas 数据框中的数据进行分组和重新排序

python - 在 PyDev 中获取交互式 Django shell

python - 使用策展人python API无法过滤和删除Elasticsearch的旧索引

python - 将列分成两部分,同时保留 pandas 中的分隔符

python - 将 panda 对象转换为 numpy 数组