Py学习  »  Python

在Python中将行值包含特定字符串移动到新列

Zephyr • 4 年前 • 822 次点击  

df = pd.DataFrame()
df ['Stats'] = ['Def duels', 'Def duels Won','Back passes', 'Back passes[Acc]','Dribbles', 'Dribbles[Suc]']
df ['Value'] = [5,2.5,60,55,5,2]

我想创建一个新列,它只包含“Won”、“Acc”和“Suc”等字符串。 预期的数据帧如下:

enter image description here

谢谢。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/57006
 
822 次点击  
文章 [ 3 ]  |  最新文章 4 年前
Datanovice
Reply   •   1 楼
Datanovice    4 年前

使用 str.contains 具有 np.where

df['stat1'] = np.where(df['Stats'].str.contains('won|acc|suc',case=False),df['Stats'],'')
df['Stats'] = np.where(df['Stats'].str.contains('won|acc|suc',case=False),'',df['Stats'])


print(df)

         Stats  Value             stat1
0    Def duels    5.0                  
1                 2.5     Def duels Won
2  Back passes   60.0                  
3                55.0  Back passes[Acc]
4     Dribbles    5.0                  
5                 2.0     Dribbles[Suc]
Quang Hoang
Reply   •   2 楼
Quang Hoang    4 年前

# initialize Stats1 with empty strings
df['Stats1'] = ''

# copy values from `Stats`
df.iloc[1::2,-1] = df['Stats']

# replace the copied values with empty strings
df['Stats'] = np.where(df['Stats1'].ne(''), '', df['Stats'])

输出:

         Stats  Value            Stats1
0    Def duels    5.0                  
1                 2.5     Def duels Won
2  Back passes   60.0                  
3                55.0  Back passes[Acc]
4     Dribbles    5.0                  
5                 2.0     Dribbles[Suc]
YOBEN_S
Reply   •   3 楼
YOBEN_S    4 年前

IIUC公司

s=df.Stats.str.contains('Won|Acc|Suc')
df['New']=df.Stats.where(s,'')
df.Stats=df.Stats.mask(s,'')
df
         Stats  Value               New
0    Def duels    5.0                  
1                 2.5     Def duels Won
2  Back passes   60.0                  
3                55.0  Back passes[Acc]
4     Dribbles    5.0                  
5                 2.0     Dribbles[Suc]