pandas 行的条件更改,具有前一行的值 [英] conditional change of a pandas row, with the previous row value
问题描述
在下面的熊猫数据框中,我想将上一行的值更改为带有 -1值的每一行。因此,这就是原始的df:
In the following pandas dataframe, I want to change each row with a "-1" value with the value of the previous row. So this is the original df:
position
0 0
1 -1
2 1
3 1
4 -1
5 0
我想将其转换为:
position
0 0
1 0
2 1
3 1
4 1
5 0
我以下面的方式进行操作,但是我认为应该有更快的方法,将其向量化或类似方法(尽管我无法做到)。
I'm doing it in the following way but I think that there should be faster ways, probably vectorizing it or something like that (although I wasn't able to do it).
for i, row in self.df.iterrows():
if row["position"] == -1:
self.df.loc[i, "position"] = self.df.loc[i-1, "position"]
因此,代码可以正常工作,但是似乎很慢,有什么办法可以加快速度吗?
So, the code works, but it seems slow, is there any way to speed it up?
推荐答案
使用替换
+ 填充
:
df.replace(-1, np.nan).ffill()
position
0 0.0
1 0.0
2 1.0
3 1.0
4 1.0
5 0.0
替换会将 -1
转换为 NaN
值。 填充
会将 NaN
s替换为正上方的值。
Replace will convert -1
to NaN
values. ffill
will replace NaN
s with the value just above it.
使用 .astype
作为整数结果:
df.replace(-1, np.nan).ffill().astype(int)
position
0 0
1 0
2 1
3 1
4 1
5 0
不要忘记将结果分配回去。您可以根据需要在非位置
上执行相同的操作。
Don't forget to assign the result back. You could perform the same operation non position
if need be:
df['position'] = df['position'].replace(-1, np.nan).ffill().astype(int)
使用 np.where
:
c = df['position']
df['position'] = np.where(c == -1, c.shift(), c)
df
position
0 0.0
1 0.0
2 1.0
3 1.0
4 1.0
5 0.0
这篇关于 pandas 行的条件更改,具有前一行的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!