亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用熊貓替換 xlsx 文件中的值

使用熊貓替換 xlsx 文件中的值

梵蒂岡之花 2023-03-08 15:55:07
我查看了其他示例并實施了它們,但沒有得到正確的結果。我有幾個看起來像這樣的數據框Player_Name   0 J.T. PostonPlayer_Name    0 J.T. Poston我正在嘗試更改名稱以匹配我擁有的另一個 excel 文件,因此我沒有使用 excel 索引手動執行此操作。這是我的代碼。import jsonimport pandas as pdimport osyear = 2018path_to_excel = '/Users/aus10/Desktop/PGA/PGA_Tour_Stats/Tournament_Results_Excel/'+str(year)+''excel_files = [pos_json for pos_json in os.listdir(path_to_excel) if pos_json.endswith('.xlsx')]for files in excel_files:    df = pd.read_excel('/Users/aus10/Desktop/PGA/PGA_Tour_Stats/Tournament_Results_Excel/'+str(year)+'/'+files+'')    df['Player_Name'].replace(to_replace='J.T. Poston', value='JT Poston')    print(df)    writer = pd.ExcelWriter('/Users/aus10/Desktop/PGA/PGA_Tour_Stats/Tournament_Results_Excel/'+str(year)+'/'+files+'', engine='xlsxwriter')    df.to_excel(writer, sheet_name='Sheet1', index=False)    df.style.set_properties(**{'text-align': 'center'})    pd.set_option('display.max_colwidth', 100)    pd.set_option('display.width', 1000)    writer.save()但是,當我在運行代碼后打開 excel 文件時,名稱沒有改變。.xlsx因為我使用的是文件而不是,是否有我遺漏的東西或我需要的特定方式.csv?
查看完整描述

1 回答

?
茅侃侃

TA貢獻1842條經驗 獲得超21個贊

這種情況下的重要參數是inplace。檢查此問題或直接在replace的文檔中找到有關此主題的注釋。

您應該足以使用此更新:

df['Player_Name'] = df['Player_Name'].replace(to_replace='J.T. Poston', value='JT Poston')

否則,您正在對副本進行替換,而不是對原始數據框進行替換。

選項 withinplace=True也應該有效:

df.replace(to_replace='J.T. Poston', value='JT Poston', inplace=True)


查看完整回答
反對 回復 2023-03-08
  • 1 回答
  • 0 關注
  • 95 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號