如何防止pandas将整数值重新设置为浮点数?

时间:2014-04-28 18:45:43

标签: python csv pandas

我是merging两个CSV文件,输入如下。

file1.csv

Id,attr1,attr2,attr3
1,True,7,"Purple"
2,False,19.8,"Cucumber"
3,False,-0.5,"A string with a comma, because it has one"
4,True,2,"Nope"
5,True,4.0,"Tuesday"
6,False,1,"Failure"

file2.csv

Id,attr4,attr5,attr6
2,"python",500000.12,False
5,"program",3,True
3,"Another string",-5,False

当我运行此代码时

import pandas as pd
df1 = pd.read_csv("file1.csv")
df2 = pd.read_csv("file2.csv")
merged = df1.merge(df2, on="Id", how="outer").fillna("")
merged.to_csv("merged.csv", index=False)

我得到像这样的输出

Id,attr1,attr2,attr3,attr4,attr5,attr6
1,True,7.0,Purple,,,
2,False,19.8,Cucumber,python,500000.12,False
3,False,-0.5,"A string with a comma, because it has one",Another string,-5.0,False
4,True,2.0,Nope,,,
5,True,4.0,Tuesday,program,3.0,True
6,False,1.0,Failure,,,

请注意,我的多条记录中的attr2已从int转换为float

1,True,7.0,Purple,,,

与预期的

相对应
1,True,7,Purple,,,

对于此示例数据集,这是一个小麻烦。但是,当我针对我的大量数据运行它时,此行为也会出现在我的Id列上。这会打破我工作流程链中的进程。

如何阻止pandas对整个文件进行此转换,或者理想情况下是针对特定列进行此转换?

1 个答案:

答案 0 :(得分:3)

您可以向dtype参数传递一个值(如果您想影响整个DataFrame,则是一种类型,还是一个字典;如果您想影响单个列):

>>> df = pd.read_csv("file1.csv", dtype={"id": int, "attr2": str})
>>> df
   id  attr1 attr2                                      attr3
0   1   True     7                                     Purple
1   2  False  19.8                                   Cucumber
2   3  False  -0.5  A string with a comma, because it has one
3   4   True     2                                       Nope
4   5   True   4.0                                    Tuesday
5   6  False     1                                    Failure

[6 rows x 4 columns]
>>> df.dtypes
id        int32
attr1      bool
attr2    object
attr3    object
dtype: object