我有一个现有的数据框,其中包含两列,如下所示:
reason market_state 0 NaN UNSCHEDULED_AUCTION 1 NaN None 2 NaN CLOSED 3 NaN CONTINUOUS_TRADING 4 NaN None 5 NaN UNSCHEDULED_AUCTION 6 NaN UNSCHEDULED_AUCTION 7 F None 8 NaN CONTINUOUS_TRADING 9 SL None 10 NaN HALTED 11 NaN None 12 NaN None 13 L None
我正在尝试将以下 3 个映射应用于上述数据框:
market_info_df['market_state'] = market_info_df['reason'].map({'F': OPENING_AUCTION}) market_info_df['market_state'] = market_info_df['reason'].map({'SL': CLOSING_AUCTION}) market_info_df['market_state'] = market_info_df['reason'].map({'L': CLOSED})
但是当我运行以上三行时,它似乎覆盖了现有的映射:
market_state reason 0 NaN NaN 1 NaN NaN 2 NaN NaN 3 NaN NaN 4 NaN NaN 5 NaN NaN 6 NaN NaN 7 NaN F 8 NaN NaN 9 NaN SL 10 NaN NaN 11 NaN NaN 12 NaN NaN 13 CLOSED L
(而且似乎已经交换了列? - 虽然这并不重要)
每行似乎都会覆盖数据框。有没有一种方法可以简单地更新数据框,即只更新三个映射,如下所示:
reason market_state 0 NaN UNSCHEDULED_AUCTION 1 NaN None 2 NaN CLOSED 3 NaN CONTINUOUS_TRADING 4 NaN None 5 NaN UNSCHEDULED_AUCTION 6 NaN UNSCHEDULED_AUCTION 7 F OPENING_AUCTION 8 NaN CONTINUOUS_TRADING 9 SL CLOSING_AUCTION 10 NaN HALTED 11 NaN None 12 NaN None 13 L CLOSED
将值连接到一个字典并按Series.fillna同一列添加market_state:
Series.fillna
market_state
d = {'F': 'OPENING_AUCTION','SL': 'CLOSING_AUCTION', 'L': 'CLOSED'} market_info_df['market_state'] = (market_info_df['reason'].map(d) .fillna(market_info_df['market_state'])) print (market_info_df) reason market_state 0 NaN UNSCHEDULED_AUCTION 1 NaN None 2 NaN CLOSED 3 NaN CONTINUOUS_TRADING 4 NaN None 5 NaN UNSCHEDULED_AUCTION 6 NaN UNSCHEDULED_AUCTION 7 F OPENING_AUCTION 8 NaN CONTINUOUS_TRADING 9 SL CLOSING_AUCTION 10 NaN HALTED 11 NaN None 12 NaN None 13 L CLOSED