调用Nan值并更改为python中的数字

时间:2017-12-28 02:36:37

标签: python pandas

我有一个DataFrame,比如df,它看起来像这样:

id            property_type1  property_type  pro
1               Condominium              2    2
2                      Farm             14   14
3                     House              7    7
4                 Lots/Land             15   15
5  Mobile/Manufactured Home             13   13
6              Multi-Family              8    8
7                 Townhouse             11   11
8             Single Family             10   10
9                 Apt/Condo              1    1
10                      Home             7    7
11                       NaN             29  NaN

现在,只要pro列的值为property_type,我就需要property_type1列与NaN列具有相同的值。这应该是这样的:

id            property_type1  property_type  pro
1               Condominium              2    2
2                      Farm             14   14
3                     House              7    7
4                 Lots/Land             15   15
5  Mobile/Manufactured Home             13   13
6              Multi-Family              8    8
7                 Townhouse             11   11
8             Single Family             10   10
9                 Apt/Condo              1    1
10                      Home             7    7
11                       NaN             29  29

也就是说,在第11行中,property_type1NaNpro列的值变为29,即property_type的值。我怎么能这样做?

2 个答案:

答案 0 :(得分:0)

查找property_type1列为NaN的行,以及这些行:将property_type值分配给pro列。

df.ix[df.property_type1.isnull(), 'pro'] = df.ix[df.property_type1.isnull(), 'property_type']

答案 1 :(得分:0)

ix已弃用,请勿使用。

选项1
我是用np.where -

执行此操作的
df = df.assign(pro=np.where(df.pro.isnull(), df.property_type, df.pro))
df
    id            property_type1  property_type   pro
0    1               Condominium              2   2.0
1    2                      Farm             14  14.0
2    3                     House              7   7.0
3    4                 Lots/Land             15  15.0
4    5  Mobile/Manufactured Home             13  13.0
5    6              Multi-Family              8   8.0
6    7                 Townhouse             11  11.0
7    8             Single Family             10  10.0
8    9                 Apt/Condo              1   1.0
9   10                      Home              7   7.0
10  11                       NaN             29  29.0

选项2
如果您要执行就地分配,请使用loc -

m = df.pro.isnull()
df.loc[m, 'pro'] = df.loc[m, 'property_type']
df

    id            property_type1  property_type   pro
0    1               Condominium              2   2.0
1    2                      Farm             14  14.0
2    3                     House              7   7.0
3    4                 Lots/Land             15  15.0
4    5  Mobile/Manufactured Home             13  13.0
5    6              Multi-Family              8   8.0
6    7                 Townhouse             11  11.0
7    8             Single Family             10  10.0
8    9                 Apt/Condo              1   1.0
9   10                      Home              7   7.0
10  11                       NaN             29  29.0

只计算一次掩码,并使用它进行多次索引,这比计算两次更有效。