我有一个系列(df中的单个col),有3个可能的值:
Stable, Increase, Decresae
,我想标记“增加”到“减少”之间的所有区域。所以对于值:
Stable Stable Stable Increase Increase Stable Stable Decrease Stable Increase Stable Decrease
我会得到:-,-,-,+,+,+,+,-,-,+,+,-最好的方法是什么?
-,-,-,+,+,+,+,-,-,+,+,-
r6l8ljro1#
可能的解决方案:
np.where (s.where(s.eq('Increase') | s.eq('Decrease')).ffill().eq('Increase'), '+', '-')
输出:
array(['-', '-', '-', '+', '+', '+', '+', '-', '-', '+', '+', '-'], dtype='<U1')
n3schb8v2#
使用Series.replace到NaN的仅Stable值,转发填充此处的现有值Increase和Decrease,并通过Increase比较numpy.where中的设置值0/-:
Series.replace
NaN
Stable
Increase
Decrease
numpy.where
0/-
df['new'] = np.where(df['col'].replace({'Stable': np.nan}).ffill().eq('Increase'), '+','-') print (df) col new 0 Stable - 1 Stable - 2 Stable - 3 Increase + 4 Increase + 5 Stable + 6 Stable + 7 Decrease - 8 Stable - 9 Increase + 10 Stable + 11 Decrease -
中间体:
print (df.assign(repl=df['col'].replace({'Stable': np.nan}), ffill=df['col'].replace({'Stable': np.nan}).ffill(), comp=df['col'].replace({'Stable': np.nan}).ffill().eq('Increase'), out=np.where(df['col'].replace({'Stable': np.nan}).ffill().eq('Increase'), '+','-'))) col repl ffill comp out 0 Stable NaN NaN False - 1 Stable NaN NaN False - 2 Stable NaN NaN False - 3 Increase Increase Increase True + 4 Increase Increase Increase True + 5 Stable NaN Increase True + 6 Stable NaN Increase True + 7 Decrease Decrease Decrease False - 8 Stable NaN Decrease False - 9 Increase Increase Increase True + 10 Stable NaN Increase True + 11 Decrease Decrease Decrease False -
qij5mzcb3#
mapTrue增加,False减少,然后ffill。最后用numpy.whereMap+/-:
map
True
False
ffill
s = df['col'].map({'Increase': True, 'Decrease': False}).ffill().fillna(False) df['indicator'] = np.where(s, '+', '-')
作为一行程序:
df['indicator'] = np.where(df['col'].map({'Increase': True, 'Decrease': False}) .ffill().fillna(False), '+', '-')
col indicator 0 Stable - 1 Stable - 2 Stable - 3 Increase + 4 Increase + 5 Stable + 6 Stable + 7 Decrease - 8 Stable - 9 Increase + 10 Stable + 11 Decrease -
col map ffill+fillna indicator 0 Stable NaN False - 1 Stable NaN False - 2 Stable NaN False - 3 Increase True True + 4 Increase True True + 5 Stable NaN True + 6 Stable NaN True + 7 Decrease False False - 8 Stable NaN False - 9 Increase True True + 10 Stable NaN True + 11 Decrease False False -
3条答案
按热度按时间r6l8ljro1#
可能的解决方案:
输出:
n3schb8v2#
使用
Series.replace
到NaN
的仅Stable
值,转发填充此处的现有值Increase
和Decrease
,并通过Increase
比较numpy.where
中的设置值0/-
:中间体:
qij5mzcb3#
map
True
增加,False
减少,然后ffill
。最后用numpy.where
Map+/-:作为一行程序:
输出:
中间体: