简体   繁体   中英

Python Dataframe : Update the values of a column in a dataframe based on another dataframe

I have a dataframe (default_act) with following columns: 'TITLE', 'activityall'

I have another dataframe (dfa) with following columns: 'id','activity','activitytype','activityinfo'

I want to set the value of 'activityinfo' column to the value from the 'activityall' column, based on the title matching:

for index, row in dfa.iterrows():
    titleori = row['activity']
    d = default_act.loc[default_act['TITLE'] == titleori]
    row['activityinfo']=d['activityall'] 

However, the values in the column 'activityinfo' have been not updated. Any help will be appreciated.

I found a solution to update the value of the column 'activityinfo' like below. However, d['activityall'] is a series, not sure how to get the values...

for index, row in dfa.iterrows():
    titleori = row['activity']
    d = default_act.loc[default_act['TITLE'] == titleori]
    dfa.set_value(index,'activityinfo',d['activityall'])

try:

for index, row in dfa.iterrows():
    titleori = row['activity']
    d = default_act.loc[default_act['TITLE'] == titleori]
    dfa.loc[index, 'activityinfo'] = d['activityall'] 
for index, row in dfa.iterrows():
    titleori = row['activity']
    d = default_act.loc[default_act['TITLE'] == titleori]
    dfa.set_value(index,'activityinfo',d['activityall'].values[0])

a bit faster solution (without looping):

dfa['activityinfo'] = dfa['activity'].map(default_act.set_index('TITLE').iloc[:, 0])

if you want to preserve values for not matching rows:

In [80]: dfa.loc[dfa.activity.isin(default_act.TITLE), 'activityinfo'] = \
   ....:     dfa['activity'].map(default_act.set_index('TITLE').ix[:, 'activityall'])

In [81]: dfa
Out[81]:
   id activity activitytype activityinfo
0   1   title1        type1    activity1
1   2   title2        type1    activity2
2   3   title3        type1    activity3
3   6   titleX        typeX          aiX

Test:

In [257]: default_act
Out[257]:
    TITLE activityall
0  title1   activity1
1  title2   activity2
2  title3   activity3
3  titleA   activityA

In [258]: dfa
Out[258]:
   id activity activitytype activityinfo
0   1   title1        type1          ai1
1   1   title1      type1_1          ai1
2   1   title1      type1_2          ai1
3   2   title2        type1          ai2
4   3   title3        type1          ai3
5   6   titleX        typeX          aiX

dfa['activityinfo'] = dfa['activity'].map(default_act.set_index('TITLE').iloc[:, 0])

Result:

In [260]: dfa
Out[260]:
   id activity activitytype activityinfo
0   1   title1        type1    activity1
1   1   title1      type1_1    activity1
2   1   title1      type1_2    activity1
3   2   title2        type1    activity2
4   3   title3        type1    activity3
5   6   titleX        typeX          NaN

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