考慮這個簡單的例子df = pd.DataFrame({'date' : [pd.to_datetime('2018-01-01'), pd.to_datetime('2018-01-01'), np.nan, pd.to_datetime('2018-01-01')], 'group' : ['a',np.nan,'b','b'], 'value' : [1,2,3,np.nan], 'value_useless' : [2,2,np.nan,2]})dfOut[101]: date group value value_useless0 2018-01-01 a 1.00000 2.000001 2018-01-01 NaN 2.00000 2.000002 NaT b 3.00000 nan3 2018-01-01 b nan 2.00000在這里,我想按組轉發填充數據框中的所有變量。根據文檔(https://pandas.pydata.org/pandas-docs/stable/generated/pandas.core.groupby.DataFrameGroupBy.fillna.html)我應該能夠做到這一點:df.groupby('group').fillna(method = 'ffill', inplace = True)Out[110]: Empty DataFrameColumns: []Index: []瘋狂的事情。這里有什么問題?謝謝!
按組填充數據框中的所有變量
幕布斯6054654
2021-06-11 15:05:14