gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 00:01:35 24 4
gpt4 key购买 nike

我是 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com