简体   繁体   中英

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 '\'

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.

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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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