[英]interate over python pandas dataframe from last row to first
I have a dataframe with statistical data that is cumulated with each new row. 我有一个数据框,其中包含每个新行累积的统计数据。 Every day a new row is added. 每天都会添加一个新行。 Now I want to iterate over my column so that each row (starting from last) is substracted from the row above. 现在我想迭代我的列,以便从上面的行中减去每一行(从最后开始)。 The new value should be put into a new column. 新值应放入新列。 This is how my dataframe looks and the values in the column 'diff' are my desired outcome: 这就是我的数据帧的外观,而'diff'列中的值是我想要的结果:
time In diff
0 2017-06-26 7.086
1 2017-06-27 8.086 1
2 2017-06-28 10.200 2.114
this is what I came up with: 这就是我提出的:
for x in df['In']:
df['diff'] = df.iloc[-1] - df.iloc[-2]
but thats not it. 但那不是。 How do I start the loop from the last row and how do I make the iloc more dynamic? 如何从最后一行开始循环,如何使iloc更具动态性? Can someone help? 有人可以帮忙吗? thank you! 谢谢!
You can use Series.diff
: 你可以使用Series.diff
:
df['diff'] = df['In'].diff()
print (df)
time In diff
0 2017-06-26 7.086 NaN
1 2017-06-27 8.086 1.000
2 2017-06-28 10.200 2.114
使用pd.Series.diff
df.assign(Diff=df.In.diff())
This can be done using shift()
: 这可以使用shift()
:
df
In time
0 7.086 2017-06-26
1 8.086 2017-06-27
2 10.200 2017-06-28
df.sort_values('time', inplace=True)
df['diff'] = df['In'] - df['In'].shift(1)
df
In time diff
0 7.086 2017-06-26 NaN
1 8.086 2017-06-27 1.000
2 10.200 2017-06-28 2.114
Here is all you need to do. 这就是你需要做的一切。
df['diff'] = df.In - df.In.shift(1)
# In [16]: df
# Out[16]:
# time In diff
# 0 2017-06-26 7.086 NaN
# 1 2017-06-27 8.086 1.000
# 2 2017-06-28 10.200 2.114
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.