pandas 导出到 CSV 时,如何在列中保留前导零?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41240535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I keep leading zeros in a column, when I export to CSV?
提问by Juliana Rivera
I am trying to export a dataframe with a column with leading zeros like this:
我正在尝试导出一个带有前导零的列的数据框,如下所示:
df["CD_LIN_NEG"]
0 001
1 001
2 004
3 001
4 001
5 001
6 003
7 006
Name: CD_LIN_NEG, dtype: object
But when I export to csv, all of the leading zeros are cut off any numbers when I open the file in Excel. How can I keep the zeros?
但是当我导出到 csv 时,当我在 Excel 中打开文件时,所有前导零都会被截断任何数字。我怎样才能保持零?
I have tried to convert to string but it doesn't work:
我试图转换为字符串,但它不起作用:
df["CD_LIN_NEG"] = df['T_PROD_CP.LN'].astype(str).apply(lambda x: x.zfill(3))
or in this way:
或以这种方式:
df["CD_LIN_NEG"] = '00' + df['T_PROD_CP.LN'].astype(str)
回答by piRSquared
This is an excel problem as @EdChum suggested. You'll want to wrap your column in =""
with apply('="{}".format)
. This will tell excel to treat the entry as a formula that returns the text within quotes. That text will be your values with leading zeros.
这是@EdChum 建议的 excel 问题。你会想=""
用apply('="{}".format)
. 这将告诉 excel 将条目视为返回引号内文本的公式。该文本将是带有前导零的值。
Consider the following example.
考虑以下示例。
df = pd.DataFrame(dict(A=['001', '002']))
df.A = df.A.apply('="{}"'.format)
df.to_excel('test_leading_zeros.xlsx')
回答by yoonghm
This may not be directly relevant to the question but if the data is read from external sources via pandas.read_csv()
or pandas.read_excel()
, then we could specify converters
for relevant columns using str
.
这可能与问题没有直接关系,但如果数据是通过pandas.read_csv()
或从外部来源读取的pandas.read_excel()
,那么我们可以converters
使用str
.
For example,
例如,
import pandas as pd
df = pd.read_excel(
'./myexcel.xlsx',
converters={
"serialno": str, # Ensure serialno is read as string, maintaining leading 0's
"location": lambda x: '-' if x=='' else str(x),
}
df1 = pd.read_excel(
'./mycsv.csv',
converters={
"serialno": str, # Ensure serialno is read as string, maintaining leading 0's
"location": lambda x: '-' if x=='' else str(x),
}
When the data is saved to Excel or CSV files, the leading 0's are maintained.
将数据保存到 Excel 或 CSV 文件时,将保留前导 0。
回答by Mohsin Asif
The most simple solution is to just add dtype=str
while reading txt
or csv
file in Pandas:
最简单的解决方案是在 Pandas 中dtype=str
读取txt
或csv
文件时添加:
df = pd.read_csv(r'C:\my_folder\my_file.csv', dtype=str)