Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python merge dataframes left_only & right_only

Tags:

python

pandas

I want to merge two dataframes by datetime; however, when there's a repeated datetime, I want to put them in separate rows instead of merge into a same row: i.e.

0   2016-10-03 11:00:00 Trade   5.0 Quote   86.70  both
1   2016-10-03 11:00:01 NaN NaN Quote   86.71 right_only

for the first row, I want to separate 'Trade' and 'Quote' into two rows:

0   2016-10-03 11:00:00 Trade   5.0 NA    Na        both
0   2016-10-03 11:00:00 Na  Na Quote    86.70   both

that is, instead of having 'both' in the indicators, I want left_only and right_only my current code:

table1 = BuyData1[['Time', 'Type','Volume']]
table1.set_index(['Time'], drop=True)
table2 = Quote_data3[['Time','Type','Price']]
table2.set_index(['Time'], drop=True)
table3 = pd.merge(table1,table2,on = 'Time', how = 'outer',sort = True, copy=True, indicator=True)
like image 849
bing Avatar asked Jun 02 '26 20:06

bing


1 Answers

Try

table1.append(table2, ignore_index=True).sort_values('Time')
like image 130
piRSquared Avatar answered Jun 05 '26 10:06

piRSquared



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!