[英]How do I convert excel file into csv file using pandas without the first row being modified?
I am trying to convert multiples excel files into csv files.我正在尝试将多个 excel 文件转换为 csv 文件。 However, when using the codes below, I am having issues with pandas modifying the values in the first row.
但是,当使用下面的代码时,我在修改第一行中的值时遇到了熊猫问题。
for i in path_list:
df = pd.read_excel(i)
df.to_csv(i[:-5]+".csv", index = None, header=True)
Below is an example of how pandas modifies the first row values.下面是 pandas 如何修改第一行值的示例。
Excel:
1 1 1 2 2 2
21 32 3 54 6 86
CSV:
1 1.1 1.2 2 2.1 2.2
21 32 3 54 6 86
How can I fix this issue so that the values in the CSV file will be exactly the same as in the excel file.如何解决此问题,以便 CSV 文件中的值与 Excel 文件中的值完全相同。
Looks like your excel
has duplicate column names.看起来你的
excel
有重复的列名。 So when you read excel
into df
, it appends .1, .2
after all duplicated column names.因此,当您将
excel
读入df
,它会在所有重复的列名之后附加.1, .2
。
You can do this to fix it:你可以这样做来修复它:
for i in path_list:
df = pd.read_excel(i)
df.columns = df.columns.astype(str).str.split('.').str[0]
df.to_csv(i[:-5]+".csv", index = None, header=True)
df.columns = df.columns.astype(str).str.split('.').str[0]
command will remove .1, .2
from duplicate column names and will keep them the way they were in excel. df.columns = df.columns.astype(str).str.split('.').str[0]
命令将从重复的列名中删除.1, .2
并保持它们在 excel 中的方式。
The same will be written in your csv
also.同样的内容也将写入您的
csv
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.