2024-04-25 11:41:02 发布
网友
假设您有一个dataframe,其中第一列是日期,连续列是随时间调整的值。F、 随着新信息的出现,特定日期的风的预测会随着时间的推移而变化。在
我的任务是计算第一列的差异。 所以原理类似于熊猫.DataFrame.diff但引用值不是前一列,而是第一列。在
所以假设你的数据帧是这样的
Date Forecast1 Forecast2 Forecast3 1/1/15 5 3 7
我希望结果是这样的:
我希望我的解释是清楚的。在
谢谢你的努力。在
您可以使用apply(..., axis=1)将其应用于行(axis=1),而不是列(默认值:axis=0):
In [78]: df Out[78]: Date Forecast1 Forecast2 Forecast3 0 1/1/15 5 3 7 1 2/3/15 1 4 5 2 3/4/15 10 2 1 In [79]: cols = [c for c in df.columns.tolist() if 'Forecast' in c] In [80]: cols Out[80]: ['Forecast1', 'Forecast2', 'Forecast3'] In [81]: df[cols].apply(lambda x: x-x[0], axis=1) Out[81]: Forecast1 Forecast2 Forecast3 0 0 -2 2 1 0 3 4 2 0 -8 -9
只需使用pd.DataFrame.sub:
In [108]: df=pd.DataFrame(np.random.randint(0,6,(3,3)), columns=['Forecast'+str(i) for i in range(1,4)], index=pd.date_range('2016/1/1',periods=3)) In [109]: df Out[109]: Forecast1 Forecast2 Forecast3 2016-01-01 5 5 5 2016-01-02 0 3 0 2016-01-03 2 4 2 In [110]: df.sub(df.Forecast1,axis=0) Out[110]: Forecast1 Forecast2 Forecast3 2016-01-01 0 0 0 2016-01-02 0 3 0 2016-01-03 0 2 0
您可以使用apply(..., axis=1)将其应用于行(axis=1),而不是列(默认值:axis=0):
只需使用pd.DataFrame.sub:
相关问题 更多 >
编程相关推荐