我想保留 df 的其他列
df=df.groupby(pd.PeriodIndex(df['date'], freq='M'))['Close/Last'].mean().reset_index()
如何將其他列保留在 df 中?
uj5u.com熱心網友回復:
我認為.agg()非常適合這里:
只要記住import numpy as np
代替:
df=df.groupby(pd.PeriodIndex(df['date'], freq='M'))['Close/Last'].mean().reset_index()
嘗試這個:
df=df.groupby(pd.PeriodIndex(df['date'], freq='M')).agg({'Close/Last': (np.mean),'Volume':'first','Name':'first'})
.agg()允許您選擇如何處理列(應用函式),但如果您只想保留它們,請使用 .agg({'col1': 'first', 'col2': 'first', ...} 等.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/456077.html
上一篇:讀取csv檔案時更改列格式
