Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Outer merging two data frames in place in pandas

How can I outer merge two data frames in place in pandas?

For example, assume we have these two data frames:

import pandas as pd

s1 = pd.DataFrame({
    'time':[1234567000,1234567005,1234567009],
    'X1':[96.32,96.01,96.05]
},columns=['time','X1'])  # to keep columns order

s2 = pd.DataFrame({
    'time':[1234567001,1234567005],
    'X2':[23.88,23.96]
},columns=['time','X2'])  # to keep columns order

They could be merged with pandas.DataFrame.merge (s3 = pd.merge(s1,s2,how='outer')) or with pandas.merge (s3=s1.merge(s2,how='outer')), but it isn't in place. Instead, I'd like the merged data frame to replace s1 in memory.

like image 619
Franck Dernoncourt Avatar asked Jul 05 '17 23:07

Franck Dernoncourt


People also ask

How to merge pandas Dataframe with outer join?

Python - Merge Pandas DataFrame with Outer Join Python Server Side Programming Programming To merge Pandas DataFrame, use the merge () function. The outer join is implemented on both the DataFrames by setting under the “how” parameter of the merge () function i.e. −

What are the different arguments to merge () in pandas?

The different arguments to merge () allow you to perform natural join, left join, right join, and full outer join in pandas. We have also seen other type join or concatenate operations like join based on index,Row index and column index.

How do I merge two DataFrames in Python?

on — If both DataFrames contain a shared column or set of columns, then you can pass these to on as keys to merge. left_on — Here, you can specify a column or list of labels that you would like to join the left DataFrame. This parameter is handy when the columns that you would like to join on in both DataFrames are named differently.

How to merge two DataFrames in row axis only?

Append is very useful when you want to merge two DataFrames in row axis only. This means that instead of matching data on their columns, we want a new DataFrame that contains all the rows of 2 DataFrames.


1 Answers

Since there is not inplace parameter in pandas.merge i think the most you can do is:

s1 = pd.merge(s1,s2,how='outer')

other than that, i don't think there's much left to do.
Hope that was helpful somehow.

like image 129
Rayhane Mama Avatar answered Oct 06 '22 16:10

Rayhane Mama