我有一个数据框,其中包含一个稀疏填充的列。大多数值都是空白的;唯一的其他价值是“买入”和“卖出”。如果最后一个非空白值为“买入”,或者如果最后一个非空白值为“卖出”,则我希望将空白值替换为“long”。我可以在一个循环中轻松地做到这一点,但我想知道是否有一种非循环的方式来完成这个?
答案 0 :(得分:0)
您可以使用fillna
或combine_first
将帮助None
创建的df
替换为replace
和ffill
(fillna
使用方法ffill
- 转发填充NaN
和None
s):
np.random.seed(12)
df = pd.DataFrame({'A':np.random.choice(['Buy','Sell', None], 10, p=(.2,.2,.6)),
'B':np.random.choice(['Buy','Sell', None], 10, p=(.2,.2,.6)),
'C':np.random.choice(['Buy','Sell', None], 10, p=(.2,.2,.6))})
print (df)
A B C
0 Buy Sell None
1 None None Buy
2 Sell None Buy
3 None None Buy
4 Buy Buy Sell
5 None None None
6 None None None
7 Buy None None
8 None None Sell
9 Buy Buy None
df = df.fillna(df.replace({'Sell':'short', 'Buy':'long'}).ffill())
#alternative solution
#df = df.combine_first(df.replace({'Sell':'short', 'Buy':'long'}).ffill())
print (df)
A B C
0 Buy Sell None
1 long short Buy
2 Sell short Buy
3 short short Buy
4 Buy Buy Sell
5 long long short
6 long long short
7 Buy long short
8 long long Sell
9 Buy Buy short
说明:
print (df.replace({'Sell':'short', 'Buy':'long'}))
A B C
0 long short None
1 None None long
2 short None long
3 None None long
4 long long short
5 None None None
6 None None None
7 long None None
8 None None short
9 long long None
print (df.replace({'Sell':'short', 'Buy':'long'}).ffill())
A B C
0 long short None
1 long short long
2 short short long
3 short short long
4 long long short
5 long long short
6 long long short
7 long long short
8 long long short
9 long long short