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

问题描述 投票:-1回答:2

我有一个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的值。我怎样才能做到这一点?

python pandas
2个回答
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

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


0
投票

找到property_type1列为NaN的行,并为这些行:将property_type值分配给pro列。

df.ix[df.property_type1.isnull(), 'pro'] = df.ix[df.property_type1.isnull(), 'property_type']
© www.soinside.com 2019 - 2024. All rights reserved.