pandas.to_csv 输出一列浮点数而不是整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33313104/
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
pandas.to_csv outputs a column of floats instead of integers
提问by Evan Cole
I'm working on project involving querying data from a dataframe, performing a few operations on it and then storing it in a csv. Here is the stripped-down code.
我正在从事涉及从数据帧查询数据、对其执行一些操作然后将其存储在 csv 中的项目。这是精简后的代码。
get_value() is a function which returns the mean of five values gotten from a query, forced into int.
get_value() 是一个函数,它返回从查询中获得的五个值的平均值,强制转换为 int。
import pandas as pd
d = pd.DataFrame(columns=['"Column1"','"Column2"'])
test = pd.read_csv("./test.csv", header = None, low_memory=False)
for line in range(1, 15):
if test.values[line][5] == '1':
value = str(get_value(line, 1))
else:
value = str(get_value(line, 0))
d.loc[line-1]=[line,value]
d.to_csv('output.csv', index = False)
Unfortunately, whenever I do so I get the first column (line, obviously an integer here) as a series of floats. Sample output:
不幸的是,每当我这样做时,我都会将第一列(行,这里显然是一个整数)作为一系列浮点数。示例输出:
1.0,4859
2.0,7882
3.0,10248
4.0,8098
5.0,8048
6.0,6087
7.0,7349
8.0,8246
9.0,5863
10.0,5962
11.0,7641
12.0,8127
13.0,7808
14.0,9886
Replacing the to_csv with a print statement gives me a dataframe full of beautiful ints:
用打印语句替换 to_csv 给了我一个充满漂亮整数的数据框:
0 1 4859
1 2 7882
2 3 10248
3 4 8098
4 5 8048
5 6 6087
6 7 7349
7 8 8246
8 9 5863
9 10 5962
10 11 7641
11 12 8127
12 13 7808
13 14 9886
As a result I suspect it's got something to do with to_csv, but I'm a novice and far from certain about that. What's going on, and is there any workaround? Thanks for reading.
因此,我怀疑这与 to_csv 有关系,但我是新手,对此还不确定。这是怎么回事,有什么解决方法吗?谢谢阅读。
Edit: DSM has helpfully suggested I run d.info(). It looks like he's right, and that they're int-looking floats.
编辑:DSM 建议我运行 d.info()。看起来他是对的,而且他们是看起来很完整的花车。
Int64Index: 14 entries, 0 to 13
Data columns (total 2 columns):
"Id" 14 non-null float64
"Sales" 14 non-null object
回答by JAB
you can change the 'floats' to 'int' via the 'astype' method:
您可以通过 'astype' 方法将 'floats' 更改为 'int':
df['id'] =df['id'].astype(int)

