处理Pandas read_csv中缺少的数据

时间:2016-10-01 23:37:22

标签: python pandas nan na missing-data

在将CSV数据导入pandas DataFrame时,我没有找到令人满意的解决方案来解决丢失数据的问题。

我有数据集,其中我事先不知道列或数据类型是什么。我希望pandas能够更好地推断如何读取数据。

我没有发现na_values=...的任何组合确实有帮助。

考虑以下csv文件:

no_holes.csv

letter,number
a,1
b,2
c,3
d,4

with_holes.csv

letter,number
a,1
,2
b, 
,4

empty_column.csv

letters,numbers
,1
,2
,3
,4

with_NA.csv

letter,number
a,1
b,NA
NA,3
d,4

当我将它们读入DataFrame(下面的代码)时会发生什么:

**no holes**
  letter  number
0      a       1
1      b       2
2      c       3
3      d       4
letter    object
number     int64
dtype: object

**with holes**
  letter number
0      a      1
1    NaN      2
2      b       
3    NaN      4
letter    object
number    object
dtype: object

**empty_column**
   letters  numbers
0      NaN        1
1      NaN        2
2      NaN        3
3      NaN        4
letters    float64
numbers      int64
dtype: object

**with NA**
  letter  number
0      a     1.0
1      b     NaN
2    NaN     3.0
3      d     4.0
letter     object
number    float64
dtype: object

有没有办法告诉pandas假设空值为object类型?我试过na_values=[""]

demo_holes.py

import pandas as pd

with_holes = pd.read_csv("with_holes.csv")
no_holes = pd.read_csv("no_holes.csv")
empty_column = pd.read_csv("empty_column.csv")
with_NA = pd.read_csv("with_NA.csv")

print("\n**no holes**")
print(no_holes.head())
print(no_holes.dtypes)
print("\n**with holes**")
print(with_holes.head())
print(with_holes.dtypes)
print("\n**empty_column**")
print(empty_column.head())
print(empty_column.dtypes)
print("\n**with NA**")
print(with_NA.head())
print(with_NA.dtypes)

1 个答案:

答案 0 :(得分:4)

您想使用参数skipinitialspace=True

设置

no_holes = """letter,number
a,1
b,2
c,3
d,4"""

with_holes = """letter,number
a,1
,2
b, 
,4"""

empty_column = """letters,numbers
,1
,2
,3
,4"""

with_NA = """letter,number
a,1
b,NA
NA,3
d,4"""

from StringIO import StringIO
import pandas as pd

d1 = pd.read_csv(StringIO(no_holes), skipinitialspace=True)
d2 = pd.read_csv(StringIO(with_holes), skipinitialspace=True)
d3 = pd.read_csv(StringIO(empty_column), skipinitialspace=True)
d4 = pd.read_csv(StringIO(with_NA), skipinitialspace=True)

pd.concat([d1, d2, d3, d4], axis=1,
          keys=['no_holes', 'with_holes',
                'empty_column', 'with_NA'])

enter image description here

如果您希望这些NaN'',请使用fillna

d1 = pd.read_csv(StringIO(no_holes), skipinitialspace=True).fillna('')
d2 = pd.read_csv(StringIO(with_holes), skipinitialspace=True).fillna('')
d3 = pd.read_csv(StringIO(empty_column), skipinitialspace=True).fillna('')
d4 = pd.read_csv(StringIO(with_NA), skipinitialspace=True).fillna('')

pd.concat([d1, d2, d3, d4], axis=1,
          keys=['no_holes', 'with_holes',
                'empty_column', 'with_NA'])

enter image description here