简体   繁体   English

从 pandas 中的列中删除部分字符串值

[英]Remove part of a string value from a column in pandas

Working with a Pandas data frame and want to be able to drop the Name ID in each row of the Players column starting with the '\'使用 Pandas 数据框并希望能够在以“\”开头的 Players 列的每一行中删除名称 ID

New to python and pandas, assuming this is probably an easy fix with a line or two of code but I cant seem to crack it. python 和 pandas 的新手,假设这可能是用一两行代码就能轻松解决的问题,但我似乎无法破解它。

file_open=open('2000_2019_All_Pro.csv')
df_AP2000 = pd.read_csv(file_open)
df_counts = df_AP2000['Player'].value_counts()
df_counts.to_frame()
df_counts.drop(df_counts.index[[0]], axis=0, inplace=True)
df_counts.reset_index().rename(columns={'index': 'Player', 'Player': 'Frequency'})

Data frame and code数据框和代码

Try using string manipulation with split :尝试将字符串操作与split结合使用:

df_counts['Player'] = df_counts['Player'].str.split('\\').str[0]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM