处理Pandas和Numpy中缺少的数据

时间:2015-09-14 13:38:26

标签: python-2.7 numpy pandas missing-data

我有以下数据样本。我想

  • a)在C栏中,替换np.NaN with 999
  • b)在D栏中,地点''与np.NaN

我的尝试都没有奏效,我不确定原因。

import pandas
from pandas import DataFrame
import numpy as np


df = DataFrame({'A' : ['foo', 'foo', 'foo', 'foo',
                        'bar', 'bar', 'bar', 'bar'],
                 'B' : ['one', 'one', 'two', 'three',
                        'two', 'two', 'one', 'three'],
                 'C' : [1, np.NaN, 1, 2, np.NaN, 1, 1, 2], 'D' : [2, '', 1, 1, '', 2, 2, 1]})

print df

df.C.fillna(999)
df.D.replace('', np.NaN)

print df

Output: 

 A      B   C  D
0  foo    one   1  2
1  foo    one NaN   
2  foo    two   1  1
3  foo  three   2  1
4  bar    two NaN   
5  bar    two   1  2
6  bar    one   1  2
7  bar  three   2  1
     A      B   C  D
0  foo    one   1  2
1  foo    one NaN   
2  foo    two   1  1
3  foo  three   2  1
4  bar    two NaN   
5  bar    two   1  2
6  bar    one   1  2
7  bar  three   2  1

1 个答案:

答案 0 :(得分:3)

这些操作返回数据的副本(大多数pandas操作行为相同),除非您明确说明(默认为inplace=False),否则它们不会就位,请参阅{{ 3}}和fillna

df.C.fillna(999, inplace=True)
df.D.replace('', np.NaN, inplace=True)

或转回:

df['C'] = df.C.fillna(999)
df['D'] = df.D.replace('', np.NaN)

此外,我强烈建议您使用下标运算符[]访问列,而不是使用点运算符.作为属性来避免模糊行为

In [60]:
df = pd.DataFrame({'A' : ['foo', 'foo', 'foo', 'foo',
                        'bar', 'bar', 'bar', 'bar'],
                 'B' : ['one', 'one', 'two', 'three',
                        'two', 'two', 'one', 'three'],
                 'C' : [1, np.NaN, 1, 2, np.NaN, 1, 1, 2], 'D' : [2, '', 1, 1, '', 2, 2, 1]})
​
df.C.fillna(999, inplace =True)
df.D.replace('', np.NaN, inplace=True)
df

Out[60]:
     A      B    C   D
0  foo    one    1   2
1  foo    one  999 NaN
2  foo    two    1   1
3  foo  three    2   1
4  bar    two  999 NaN
5  bar    two    1   2
6  bar    one    1   2
7  bar  three    2   1