从其他两列替换一列中的NaN值

时间:2020-10-28 18:09:18

标签: python pandas dataframe nan

我有一个数据框,它是三个连接在一起的数据框。我有变量表示它们来自哪个数据帧。例如,DAY_OF_WEEK_summer1DAY_OF_WEEK_summer2DAY_OF_WEEK_summer3。一个值只能存在于这三列之一中,我想用summer2或summer3列中的值填充NaN中的DAY_OF_WEEK_summer1值。我总共要填充NaN个值中的11个属性。
这是一个示例数据框:

df = pd.DataFrame({
    'DAY_OF_WEEK_summer1': [np.nan, 'WKDY', 'SAT', np.nan, np.nan],
    'DAY_OF_WEEK_summer2': [np.nan, np.nan, np.nan, 'WKDY', 'WKDY'],
    'DAY_OF_WEEK_summer3': ['SAT', np.nan, np.nan, np.nan, np.nan],
    'ROUTE_summer1': [np.nan, 5, 6, np.nan, np.nan],
    'ROUTE_summer2': [np.nan, np.nan, np.nan, 10, 10],
    'ROUTE_summer3': [1, np.nan, np.nan, np.nan, np.nan]
})

我希望结果看起来像这样:

DAY_OF_WEEK_summer1  |  DAY_OF_WEEK_summer2  |  DAY_OF_WEEK_summer3  |  ROUTE_summer1 |  ROUTE_summer2   | ROUTE_summer3
---------------------+-----------------------+-----------------------+----------------+------------------+---------------
       SAT           |         NaN           |          SAT          |     1          |     NaN          |        1
       WKDY          |         NaN           |          NaN          |     5          |     NaN          |        NaN
       SAT           |         NaN           |          NaN          |     6          |     NaN          |        NaN
       WKDY          |         WKDY          |          NaN          |     10         |     10           |        NaN
       WKDY          |         WKDY          |          NaN          |     10         |     10           |        NaN

1 个答案:

答案 0 :(得分:0)

import numpy as np

df['DAY_OF_WEEK_summer1'] = np.where(df['DAY_OF_WEEK_summer1'].isnull(), df['DAY_OF_WEEK_summer2'], df['DAY_OF_WEEK_summer1'])
df['DAY_OF_WEEK_summer1'] = np.where(df['DAY_OF_WEEK_summer1'].isnull(), df['DAY_OF_WEEK_summer3'], df['DAY_OF_WEEK_summer1'])