Python 使用Pandas,如何按天对JSON数据进行分组并计算出现的次数?
我知道这个问题看起来有点荒谬,但我今天整天都在寻找它的解决方案,结果一无所获 我有一个JSON对象Python 使用Pandas,如何按天对JSON数据进行分组并计算出现的次数?,python,pandas,Python,Pandas,我知道这个问题看起来有点荒谬,但我今天整天都在寻找它的解决方案,结果一无所获 我有一个JSON对象 times = [1554278809, 1554276258, 1554274173, 1554270457] /* four occurrences in April 3 */ 我需要按天对其进行分组并计算发生率,得到如下结果: result = [{'2019-04-01': 0, '2019-04-02': 0, '2019-04-03': 4}] 到目前为止,我已经尝试过这个(正如预
times = [1554278809, 1554276258, 1554274173, 1554270457] /* four occurrences in April 3 */
我需要按天对其进行分组并计算发生率,得到如下结果:
result = [{'2019-04-01': 0, '2019-04-02': 0, '2019-04-03': 4}]
到目前为止,我已经尝试过这个(正如预期的那样,它不起作用):
使用convert unix time创建
系列
,然后通过以下方式将其转换为字符串:
times = [str(datetime.fromtimestamp(x)) for x in times]
df = pd.DataFrame(times)
print(df.groupby(df.index.date).count())
times = [1554278809, 1554276258, 1554274173, 1554270457]
s = pd.Series(pd.to_datetime(times, unit='s'))
print (s)
0 2019-04-03 08:06:49
1 2019-04-03 07:24:18
2 2019-04-03 06:49:33
3 2019-04-03 05:47:37
dtype: datetime64[ns]
print(s.groupby(s.dt.strftime('%Y-%m-%d')).count().to_dict())
{'2019-04-03': 4}