簡體   English   中英

從 pandas 中的列中刪除部分字符串值

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

使用 Pandas 數據框並希望能夠在以“\”開頭的 Players 列的每一行中刪除名稱 ID

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'})

數據框和代碼

嘗試將字符串操作與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