我有一个数据框如下:
Datetime Value
--------------------------------------------
2000-01-01 15:00:00 10
2000-01-01 16:00:00 12
2000-01-01 17:00:00 14
2000-01-01 18:00:00 16
2000-01-02 15:00:00 13
2000-01-02 16:00:00 18
2000-01-02 17:00:00 16
2000-01-02 18:00:00 15
--------------------------------------------
我想得到一个列,我可以在其中获取从特定时间开始的每一天(比如 16:00:00)的值的差异,如下所示:
Datetime Value NewColumn
--------------------------------------------
2000-01-01 15:00:00 10 -
2000-01-01 16:00:00 12 0
2000-01-01 17:00:00 14 2
2000-01-01 18:00:00 16 4
2000-01-02 15:00:00 13 -
2000-01-02 16:00:00 18 0
2000-01-02 17:00:00 16 -2
2000-01-02 18:00:00 15 -3
--------------------------------------------
我试过下面的代码,但它显示错误:
df['NewColumn'] = df.groupby('Datetime')['Value'].apply(lambda x: x - df.loc[(df['Datetime'].dt.time == dt.time(hour=16)), 'Value'])
ValueError: Buffer dtype mismatch, expected 'Python object' but got 'long long'
我应该如何编写代码?
最佳答案
IIUC,这就是你需要的。
df['Datetime']=pd.to_datetime(df['Datetime'])
df['NewColumn'] = (df.groupby(pd.Grouper(freq='D', key='Datetime'))['Value']
.apply(lambda x: x - df.loc[x.loc[df['Datetime'].dt.hour == 16].index[0],'Value']))
df.loc[df['Datetime'].dt.hour < 16, 'NewColumn'] = '-'
print(df)
输出
Datetime Value NewColumn
0 2000-01-01 15:00:00 10 -
1 2000-01-01 16:00:00 12 0
2 2000-01-01 17:00:00 14 2
3 2000-01-01 18:00:00 16 4
4 2000-01-02 15:00:00 13 -
5 2000-01-02 16:00:00 18 0
6 2000-01-02 17:00:00 16 -2
7 2000-01-02 18:00:00 15 -3
关于python - DataFrame.groupby.apply() 与 lambda 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58273919/