Home > Enterprise >  Python two csv with similar content in one dataframe with diffrent Dates and diffrent length [duplic
Python two csv with similar content in one dataframe with diffrent Dates and diffrent length [duplic

Time:10-05

I want to add two csv's to one dataframe. The 2 Dataframes are similar but not the same. The Date is about 10 min apart. The length can be diffrent two. I need a left joint but how can I make wehen ther is no data it should be replaced with a zero or a null?

out1.csv:

date,speed
2021-10-03 02:00:01,5.0
2021-10-03 02:00:02,5.2
2021-10-03 02:00:03,5.1

out2.csv:

date,curret
2021-10-03 02:00:02,32.012
2021-10-03 02:00:03,32.12
2021-10-03 02:00:04,32.5

As a outpu I need a dataframe something like that:

date,speed,current
2021-10-03 02:00:01,5.0,null
2021-10-03 02:00:02,5.2,32.012
2021-10-03 02:00:03,5.1,32.12
2021-10-03 02:00:04,null,32.5

CodePudding user response:

There is on parameter in the join() method. As per doc,

Another option to join using the key columns is to use the on parameter. DataFrame.join always uses other’s index but we can use any column in df. This method preserves the original DataFrame’s index in the result.

df.join(other.set_index('key'), on='key')
  key   A    B
0  K0  A0   B0
1  K1  A1   B1
2  K2  A2   B2
3  K3  A3  NaN
4  K4  A4  NaN
5  K5  A5  NaN

CodePudding user response:

Use outer join - df1.merge(df2, how="outer")

See documentation here

  • Related