pandas系列标记两个值之间的所有行

7kqas0il  于 2023-06-20  发布在  其他
关注(0)|答案(3)|浏览(128)

我有一个系列(df中的单个col),有3个可能的值:

Stable, Increase, Decresae

,我想标记“增加”到“减少”之间的所有区域。所以对于值:

Stable
Stable
Stable
Increase
Increase
Stable
Stable
Decrease
Stable
Increase
Stable
Decrease

我会得到:-,-,-,+,+,+,+,-,-,+,+,-最好的方法是什么?

r6l8ljro

r6l8ljro1#

可能的解决方案:

np.where
(s.where(s.eq('Increase') | s.eq('Decrease')).ffill().eq('Increase'),
 '+', '-')

输出:

array(['-', '-', '-', '+', '+', '+', '+', '-', '-', '+', '+', '-'],
      dtype='<U1')
n3schb8v

n3schb8v2#

使用Series.replaceNaN的仅Stable值,转发填充此处的现有值IncreaseDecrease,并通过Increase比较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   -
qij5mzcb

qij5mzcb3#

mapTrue增加,False减少,然后ffill。最后用numpy.whereMap+/-:

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         -

相关问题