默认情况下,pd.read_csv将整数视为浮点型 [英] pd.read_csv by default treats integers like floats
问题描述
我有一个csv
看起来像(标题=第一行):
I have a csv
that looks like (headers = first row):
name,a,a1,b,b1
arnold,300311,arnld01,300311,arnld01
sam,300713,sam01,300713,sam01
当我跑步时:
df = pd.read_csv('file.csv')
列a
和b
的末尾带有.0
,如下所示:
Columns a
and b
have a .0
attached to the end like so:
df.head()
name,a,a1,b,b1
arnold,300311.0,arnld01,300311.0,arnld01
sam,300713.0,sam01,300713.0,sam01
列a
和b
是整数或空格,那么为什么pd.read_csv()
像对待浮点数一样对待它们,以及如何确保它们在读取时是整数?
Columns a
and b
are integers or blanks so why does pd.read_csv()
treat them like floats and how do I ensure they are integers on the read?
推荐答案
As root mentioned in the comments, this is a limitation of Pandas (and Numpy). NaN
is a float and the empty values you have in your CSV are NaN.
这在 gotchas 中列出熊猫.
您可以通过几种方法解决此问题.
You can work around this in a few ways.
在下面的示例中,我使用以下内容导入数据-请注意,我在a
和b
For the examples below I used the following to import the data - note that I added a row with an empty value in columns a
and b
import pandas as pd
from StringIO import StringIO
data = """name,a,a1,b,b1
arnold,300311,arnld01,300311,arnld01
sam,300713,sam01,300713,sam01
test,,test01,,test01"""
df = pd.read_csv(StringIO(data), sep=",")
删除NaN行
您的第一个选择是删除包含此NaN
值的行.不利的一面是您输掉了整行.将数据放入数据框后,运行以下命令:
Drop NaN rows
Your first option is to drop rows that contain this NaN
value. The downside of this, is that you lose the entire row. After getting your data into a dataframe, run this:
df.dropna(inplace=True)
df.a = df.a.astype(int)
df.b = df.b.astype(int)
这将从数据框中删除所有NaN
行,然后将列a
和列b
转换为int
This drops all NaN
rows from the dataframe, then it converts column a
and column b
to an int
>>> df.dtypes
name object
a int32
a1 object
b int32
b1 object
dtype: object
>>> df
name a a1 b b1
0 arnold 300311 arnld01 300311 arnld01
1 sam 300713 sam01 300713 sam01
用占位符数据填充NaN
此选项将用扔掉值替换所有NaN
值.该值是您需要确定的.对于此测试,我将其设置为-999999
.这将允许使用它来保留其余数据,将其转换为int,并使其清楚哪些数据无效.如果您稍后要根据各列进行计算,则可以将这些行过滤掉.
Fill NaN
with placeholder data
This option will replace all your NaN
values with a throw away value. That value is something you need to determine. For this test, I made it -999999
. This will allow use to keep the rest of the data, convert it to an int, and make it obvious what data is invalid. You'll be able to filter these rows out if you are making calculations based on the columns later.
df.fillna(-999999, inplace=True)
df.a = df.a.astype(int)
df.b = df.b.astype(int)
这将产生一个数据帧,如下所示:
This produces a dataframe like so:
>>> df.dtypes
name object
a int32
a1 object
b int32
b1 object
dtype: object
>>> df
name a a1 b b1
0 arnold 300311 arnld01 300311 arnld01
1 sam 300713 sam01 300713 sam01
2 test -999999 test01 -999999 test01
保留浮点值
最后,另一种选择是保留浮点值(和NaN
),而不用担心非整数数据类型.
Leave the float values
Finally, another choice is to leave the float values (and NaN
) and not worry about the non-integer data type.
这篇关于默认情况下,pd.read_csv将整数视为浮点型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!