python - 使用熊猫替换 xlsx 文件中的值
问题描述
我查看了其他示例并实现了它们,但我没有得到正确的结果。我有几个看起来像这样的数据框
Player_Name
0 J.T. Poston
Player_Name
0 J.T. Poston
我正在尝试更改名称以匹配我拥有的另一个 excel 文件,因此我没有使用 excel 索引手动执行此操作。这是我的代码。
import json
import pandas as pd
import os
year = 2018
path_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
?
解决方案
推荐阅读
- c++ - 一个数字在所有查询中出现的次数?
- javascript - JS中的变量是对象吗
- c++ - 如何在 C++ 中使用矢量打印出特定员工的记录?
- c++ - 如何在普通数组的单元格中插入结构或指针?C++
- javascript - 如何通过在javascript中调用函数在对象内添加属性
- javascript - 如果选中其他单选按钮,请检查单选按钮
- html - 类型号不适用于小数位
- css - 在我的情况下,我必须为一行使用 5 col-md,另一个 col-md 需要在没有打开行的情况下进入下一行
- python - 如何将 dict keyx:valuex 列表转换为 dict key:keyx, value:valuex 列表?
- vue-material - 未在 Vue Material md-tabs 中列出的页面会抛出“未捕获的类型错误:无法读取属性 'parentNode' of null”