简体   繁体   中英

Replace pandas dataframe column with another dataframe column with condition

I have some DataFrame, in the first one the columns just include id's, like below:

df1:

f_id  l_id  p_id s_id
0       0    1 1
1       1    2 2

In other DataFrames, I have the id column of first one and those data:

df2:

f_id    f_name
0       Johen
1       Joe


df3:

l_id    l_name
0       Ardi
1       Peykan

I want to replace the id column with the data, for example instead of f_id 0 place John

I want this:

result:

f_id   l_id   p_id  s_id
Johen   Ardi    1     1 
Joe    Peykan   2     2

DataFrames lengths are not same and the id's has been meesed up.

If you want to update by position/index: you canconcat all your dataframes to a single one after reshaping to Series. Thenupdate df1 in place:

dfs = [df2, df3]
df1.update(pd.concat([d.set_index(d.columns[0]).squeeze().rename(d.columns[0])
                      for d in dfs], axis=1))

output:

    f_id    l_id  p_id  s_id
0  Johen    Ardi     1     1
1    Joe  Peykan     2     2

proof of reproducibility:

df1 = pd.DataFrame({'f_id': [0, 1], 'l_id': [0, 1], 'p_id': [1, 2], 's_id': [1, 2]})
df2 = pd.DataFrame({'f_id': [0, 1], 'f_name': ['Johen', 'Joe']})
df3 = pd.DataFrame({'l_id': [0, 1], 'l_name': ['Ardi', 'Peykan']})

print('Original')
print(df1)

dfs = [df2, df3]
df1.update(pd.concat([d.set_index(d.columns[0]).squeeze().rename(d.columns[0])
                      for d in dfs], axis=1))

print('\nAfter update:')
print(df1)

output:

Original
   f_id  l_id  p_id  s_id
0     0     0     1     1
1     1     1     2     2

After update:
    f_id    l_id  p_id  s_id
0  Johen    Ardi     1     1
1    Joe  Peykan     2     2

alternative (not what is wanted)

Or, if you want to use the original value as key:

dfs = [df2, df3]
sub = {d.columns[0]: d.set_index(d.columns[0]).squeeze() for d in dfs}
df1.update(df[df1.columns.intersection(sub)].apply(lambda d: d.map(sub[d.name])))

output:

    f_id    l_id  p_id  s_id
0  Johen  Peykan     1     1
1    Joe     1.1     2     2

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