简体   繁体   中英

How to fill a column of a dataframe (df1) with info from another dataframe (df2)? Just when two column info matches in df1 and df2?

I have a df1 with 10k rows like:

name  time  day  details  year

xxx    1    mon   AA

yyy    2    tue   BB

zzz    3    mon   CC

And i have a df2 with 2k rows like:

  time   details  year
   4      AA       1900

   2      BB       2000

   5      CC       2030

When time and details of row of df1 is equal to time and detals of the row in df2, i want get the year info of df2 and update df1. The desire df is like that:

name  time  day  details  year
    
xxx    1    mon   AA

yyy    2    tue   BB   2000

zzz    3    mon   CC

Try a 'left' merge on 'time' and 'details':

import numpy as np
import pandas as pd

df1 = pd.DataFrame({
    'name': ['xxx', 'yyy', 'zzz'],
    'time': [1, 2, 3],
    'day': ['mon', 'tue', 'mon'],
    'details': ['AA', 'BB', 'CC'],
    'year': [np.nan, np.nan, np.nan]
})

df2 = pd.DataFrame({
    'time': [4, 2, 5],
    'details': ['AA', 'BB', 'CC'],
    'year': [1900, 2000, 2030]
})

merged = df1.drop(columns='year').merge(df2, on=['time', 'details'], how='left')

print(merged)

merged :

  name  time  day details    year
0  xxx     1  mon      AA     NaN
1  yyy     2  tue      BB  2000.0
2  zzz     3  mon      CC     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