简体   繁体   中英

Adding value from one pandas dataframe to another dataframe by matching a variable

Suppose I have a pandas dataframe df with 2 columns

           c1            c2
    0      v1            b1
    1      v2            b2
    2      v3            b3
    3      v4            b4
    4      v5            b5

A second dataframe, df2 contains c1, c2 and a few other columns.

   c1 c2 c3  c4
0  "" b5 500 3
1  "" b2 420 7
2  "" b1 380 5
3  "" b2 470 9
4  "" b3 290 2

My goal is to replace the empty values for c1 in df2, with those in df, corresponding to the values in c2, so the first five values for c1 in df2, should be v5,v2,v1,v2 and v3 respectively. What is the best way to do this?

One easy way you can do is to use the merge of pandas based on similar column.

df2.drop('c1', axis=1, inplace=True)
main_df = pd.merge(df2, df, on="c2", how="left")
df2['c1'] = main_df['c1']
df2.columns = ['c1','c2','c3','c4']

Something like this might be what you're looking for:

import pandas as pd
import numpy as np
# remove the c1 column from df2
df2.drop("c1", axis=1, inplace=True)
# merge the 2 dataframes and get the c1 values corresponding to c2
newdf = df.merge(df2, on="c2")

If you actually have missing values in the column (ie NAs and some rows where values are present, the solution will be different)

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