使用 Pandas 导出到 csv 时如何指定数据类型和格式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36344941/
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 to specify the data type and format when exporting to csv using Pandas?
提问by Alex Kinman
I have a pandas data frame were currently all the columns are floats, which I am exporting to a csv file using DF.to_csv.
我有一个Pandas数据框,目前所有的列都是浮点数,我正在使用 DF.to_csv 将其导出到 csv 文件。
I would like for one of the columns to be exported as an int instead of as a float. A second columns has numbers with a lot of decimals and is being exported in scientific notation format. I want to to be exported as a regular decimal number up to a certain degree of precision, not in scientific notation.
我希望将其中一列导出为 int 而不是浮点数。第二列的数字有很多小数,并以科学记数法格式导出。我想导出为具有一定精度的常规十进制数,而不是科学记数法。
Say my DF is called DataOut and has columns 'A', 'B' and 'C'
假设我的 DF 被称为 DataOut 并且有“A”、“B”和“C”列
Is there anything I can add to
有什么我可以补充的吗
DataOut.to_csv(filename, mode = 'w', header = False , index=False)
So that the values in A are exported as int, and the values in B are exported as decimals with a maximum precision of 20 digits ?
以便 A 中的值导出为 int,B 中的值导出为最大精度为 20 位的小数?
回答by Sam
Make a copy of your dataframe, round the respective columns to ints, and export the CSV:
复制您的数据框,将相应的列四舍五入为整数,然后导出 CSV:
import pandas as pd
import random
#generate a dataframe with two columns, both floating point
df = pd.DataFrame({'A':[random.random()*10 for i in range(10)],
'B':[random.random()*20 for i in range(10)]})
df2 = df.copy() #make a copy to preserve your original
df2.loc[:, 'A'] = df2['A'].apply(int) #convert A to an int
df2.loc[:, 'B'] = df2['B'].round(20) #round B to 20 points of precision
df2.to_csv('test.csv', header = None, index = False)
回答by Milor123
for the float
对于浮动
Which works similarly for to_csv:
df.to_csv('df.csv', float_format='{:f}'.format, encoding='utf-8')
Source https://stackoverflow.com/a/23006399/4941927Probably with float_format also can convert to int, but i dont know.
来源https://stackoverflow.com/a/23006399/4941927可能用 float_format 也可以转换为 int,但我不知道。
for the int convertion I think that could use a round() function and a generator before parser to plain file, but i'm sure because i never use panda
对于 int 转换,我认为可以在解析器到纯文件之前使用 round() 函数和生成器,但我敢肯定,因为我从不使用Pandas
http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_csv.html
http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_csv.html
I would see your complete code @AlexKinman
我会看到你的完整代码@AlexKinman