在pandas DataFrame中,我试图用一个单一名称重新标记变量的两个级别,但不更改变量中的“ Nan”值。

以下是使用修改版的“ mtcars”数据集的可复制示例。在这里,我想将“ am”变量的“是”和“否”级别重新标记为“新”。

                    mpg   cyl  disp  hp drat    wt  qsec vs  am
Mazda RX4           21.0  six 160.0 110 3.90 2.620 16.46  0  yes
Mazda RX4 Wag       21.0  two 160.0 110 3.90 2.875 17.02  0  NaN
Datsun 710          22.8  six 108.0  93 3.85 2.320 18.61  1  no
Hornet 4 Drive      21.4  two 258.0 110 3.08 3.215 19.44  1  NaN
Hornet Sportabout   18.7  six 360.0 175 3.15 3.440 17.02  0  yes
Valiant             18.1  two 225.0 105 2.76 3.460 20.22  1  NaN
Duster 360          14.3  two 360.0 245 3.21 3.570 15.84  0  no


结果将如下所示:

                    mpg   cyl  disp  hp drat    wt  qsec vs  am
Mazda RX4           21.0  six 160.0 110 3.90 2.620 16.46  0  new
Mazda RX4 Wag       21.0  two 160.0 110 3.90 2.875 17.02  0  NaN
Datsun 710          22.8  six 108.0  93 3.85 2.320 18.61  1  new
Hornet 4 Drive      21.4  two 258.0 110 3.08 3.215 19.44  1  NaN
Hornet Sportabout   18.7  six 360.0 175 3.15 3.440 17.02  0  new
Valiant             18.1  two 225.0 105 2.76 3.460 20.22  1  NaN
Duster 360          14.3  two 360.0 245 3.21 3.570 15.84  0  new

最佳答案

尝试:

  mt['am'] = mt.am.map(lambda x: x if pd.isnull(x) else 'new')


输出:

In [21]: df = pd.DataFrame(['yes',np.nan,'no',np.nan], columns=['am'])

In [22]: df
Out[22]:
    am
0  yes
1  NaN
2   no
3  NaN

In [23]: df['am'] = df.am.map(lambda x: x if pd.isnull(x) else 'new')

In [24]: df
Out[24]:
    am
0  new
1  NaN
2  new
3  NaN

关于python - 重新标记 Pandas 的水平,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31121803/

10-10 14:35