不能按时间表制作熊猫组

时间:2018-03-21 21:08:40

标签: python pandas datetime pandas-groupby

提前感谢任何输入......

我正在尝试将一些温度读数组合在一起,这样我就可以比较两组读数。下面我试着做一分钟,但是如果它是一个小时就没关系。

数据似乎通过OK,只是不能正确分组。它只列出了所有的温度读数(每分钟很多)。

数据在此处:https://github.com/uilfut/pi_code/blob/master/tinydb_environment.json

'date_time'是日期时间列

'tempC'是温度栏

from pandas.io.json import json_normalize
import datetime as dt
import numpy as np
with open('/home/data.json') 
as f:
    data = json.load(f)

df = pd.DataFrame.from_dict({(i, j): data[i][j] for i in data.keys() 
for j in data[i].keys()}, orient='index')
df['data_source'] = df.index.droplevel(level=1)
df.index = df.index.droplevel(level=0)

df['date_time'] = pd.to_datetime(df['date_time'])

# this next line doesn't work - 
df.set_index('date_time').groupby(pd.Grouper(freq='T, min')['tempC'].mean()

print(df.head(10))

我也试过

# df['time'] = df['date_time'].dt.time
# df.groupby(pd.TimeGrouper('5Min'))['tempC'].mean().dropna()
# df.set_index('date_time').groupby(pd.TimeGrouper('1hour'))
['tempC'].mean().dropna()
# df.set_index('date_time').groupby(pd.Grouper(freq='T, min'))
['tempC'].mean()
# df.set_index('time').resample("H").mean()
# df.set_index('date_time').resample('5min', how=np.mean)

希望这对某人来说很容易!

非常感谢

1 个答案:

答案 0 :(得分:0)

Your groupby line is missing a ), and pandas 0.22 doesn't recognize the frequency T, min. Try this:

df.set_index('date_time').groupby(pd.Grouper(freq='min'))['tempC'].mean()