简体   繁体   中英

How to use isin to fill in values in a pandas dataframe?

hlist = pd.DataFrame({'ID': [2122,2123,23,111,2122]})

df = pd.DataFrame({'ID': [2122,2123,23,111,2122],

                   'num_wings': [2, 0, 0, 0,2],})

I am attempting to create a new column in the df dataframe and populate it with the value 'high', if the ID appears in the hlist database. I tried this:

df.loc[df[df.ID.isin(hlist.ID)], 'BDI_Cat'] = 'high'

But am getting this error: ValueError: Index data must be 1-dimensional

Adding

df.loc[df.ID.isin(hlist.ID), 'BDI_Cat'] = 'high'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM