简体   繁体   English

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

[英]Moving row values contains specific string to new column in Python

I am restructuring the data frame.我正在重组数据框。 The sample data frame is as follow:样本数据框如下:

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]

I want to create a new column which only contains the string such as 'Won','Acc' and 'Suc'.我想创建一个新列,它只包含诸如“Won”、“Acc”和“Suc”之类的字符串。 The expected data frame is as follow:预期的数据框如下:

在此处输入图片说明

What can I try to resolve this?我可以尝试什么来解决这个问题?

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]

A solution:一个办法:

# 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'])

Output:输出:

         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]

using str.contains with np.where使用str.containsnp.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]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 复制包含行上的字符串并将特定结果移动到新列 Pandas python - Replicate contains string on row and move with specific result to new column Pandas python 如果一行包含特定字符串,pandas 是否可以在新列中创建一个整数 - Is there a way in pandas to create an integer in a new column if a row contains a specific string 如果列包含 Python 中的特定字符串,则从列中删除值 - Remove values from columns if a column contains specific string in Python 如果 DataFrame 包含特定字符串,则创建新列 - Create new column if DataFrame contains specific string Python - 如果列名包含特定字符串,则更改该列中的值,否则值保持不变 - Python - If column name contains a specific string then change the values in that column otherwise the values remain 使用 Python 中的循环根据字符串值修改新列中的行值 - Modify row values in new column based on string value with loop in Python 如何总结列中包含特定字符串的行中的所有值? - How to sum up all values in a row where the column contains a specific string? 使用字符串添加新列包含 python - Adding new column using string contains python 将数据从特定列移动到新行中的不同列 - Moving data from specific columns to a different column in a new row 在 python 中,如果单元格包含特定字符串,则删除特定字符串并同时将特定字符串移动到不同的单元格 - In python, If cell contains specific string, remove specific string and while moving specific string to a different cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM