簡體   English   中英

DataFrame.groupby.apply() 與 lambda 功能

[英]DataFrame.groupby.apply() with lambda functions

我有一個 dataframe 如下:

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)

Output

              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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM