简体   繁体   中英

Add data from a dataframe to another

I'd like to add data from a dataframe to a another dataframe.

Here my first data frame: df

在此处输入图片说明

Here my second dataframe: dff

在此处输入图片说明

df.append(dff, ignore_index=True, sort=False)

And it results me that:

在此处输入图片说明

Appending is adding two dataframe 'on top of each other' so you add rows. If you want to add new columns to your dataframe from another dataframe, you use join or merge especially if you have a key column where you can merge, which is in your case Postal Code

In pandas we use pd.merge for merging two dataframes with each other like the followng:

df_final = pd.merge(df, dff, on='Postal Code')


    Postal Code     Borough     Neighbourhood   Lattitude   Longitude
0   M1B             Scarborough Rouge           43.806686   -79.194353
1   M1B             Scarborough Malvern         43.806686   -79.194353
2   M1C             Scarborough Port Union      43.784535   -79.160497
3   M1C             Scarborough Rouge Hill      43.784535   -79.160497
4   M1C             Scarborough Highland Creek  43.784535   -79.160497

I think merge is what you're looking for.

df.merge(dff, on='Postal Code')

should do the trick.

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