pandas 如何更新现有 Dataframe 以添加值,而不覆盖同一列中的其他现有值?

vbkedwbf  于 2022-11-27  发布在  其他
关注(0)|答案(2)|浏览(201)

我有一个现有的 Dataframe ,其中有两列,如下所示:

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个Map应用到上述 Dataframe :

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})

但是当我运行上面的3行代码时,它似乎覆盖了现有的Map:

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

(And它似乎交换了列?-尽管这并不重要)
每一行似乎都覆盖了 Dataframe 。是否有一种方法可以简单地更新 Dataframe ,即只更新三个Map,如下所示:

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
34gzjxbg

34gzjxbg1#

将值连接到一个字典,并通过相同的列market_state添加Series.fillna

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
chhqkbe1

chhqkbe12#

使用单个字典,然后根据需要使用原始值fillna

market_info_df['market_state'] = (
 market_info_df['reason']
  .map({'F': 'OPENING_AUCTION',   # only ONE dictionary
        'SL': 'CLOSING_AUCTION',
        'L': 'CLOSED'})
 .fillna(market_info_df['market_state'])
)

或者,若要仅更新NA值:

df.loc[df['market_state'].isna(), 'market_state'] = (
    market_info_df['reason']
     .map({'F': 'OPENING_AUCTION',   # only ONE dictionary
           'SL': 'CLOSING_AUCTION',
           'L': 'CLOSED'})
)

输出量:

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

相关问题