简体   繁体   中英

Merging two Pandas DataFrames on specific condition

I have two Pandas dataframes, and I want to merge them on specific condition. These are my dataframes:

import pandas as pd

pd.set_option('display.max_rows', 250)
pd.set_option('display.max_columns', 7)
pd.set_option('display.width', 800)


df1 = pd.DataFrame({"food":["fruit", "fruit", "fruit"],
                    "name":["apple", "grape", "bannana"]})
print(df1)


df2 = pd.DataFrame({"name":["apple", "apple", "apple", "grape", "grape"],
                    "color":["red", "green", "yellow","white", "blue"]})
print(df2)

They look like this:

    food     name
0  fruit    apple
1  fruit    grape
2  fruit  bannana

    name   color
0  apple     red
1  apple   green
2  apple  yellow
3  grape   white
4  grape    blue

I want from my result dataframe to look like this:

    food   name   color
0  fruit  apple     red
1  fruit  apple   green
2  fruit  apple  yellow
3  fruit  grape   white
4  fruit  grape    blue

So I want to merge them on "name" column but I want to drop nan values. How can I do that?

You can use .merge to join the dataframes, and the use .dropna to drop the rows with NA values.

df1.merge(df2, how='left', on='name').dropna()
# returns:
    food   name   color
0  fruit  apple     red
1  fruit  apple   green
2  fruit  apple  yellow
3  fruit  grape   white
4  fruit  grape    blue

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