2

I have a dataframe with a range index and no data, in real data the index is a time range.

E.g.

df_main = pd.DataFrame(index = pd.RangeIndex(0,15,1))

See Fig1

And I have several dataframes which varying columns and indexes, I just want to join those on the main dataframe based on index:

df1 = pd.DataFrame({'value': [1, 2, 3, 5]}, index = pd.RangeIndex(0,4,1))
df2 = pd.DataFrame({'value': [5, 6, 7, 8]}, index = pd.RangeIndex(4,8,1))
df3 = pd.DataFrame({'value2': [9, 8, 7, 6]}, index = pd.RangeIndex(0,4,1))
df4 = pd.DataFrame({'value': [1, 2],'value2': [3, 4],'value3': [5, 6]}, index = pd.RangeIndex(10,12,1))

See Fig 2,3,4,5

I tried concat:

display(pd.concat([df_main,df1,df2,df3,df4]))

Which gives me the unwanted output you can see in Fig 6.

I also tried join which results in an error I did not understand:

ValueError: Indexes have overlapping values: Index(['value', 'value2'], dtype='object')

What I want to is the output you can see in Fig7.

Figures

1
  • try pandas merge method Commented Jun 7, 2019 at 9:44

2 Answers 2

3

You could groupby the index and aggregate with first:

pd.concat([df_main, df1, df2, df3, df4]).groupby(level=0).first()

[out]

    value  value2  value3
0     1.0     9.0     NaN
1     2.0     8.0     NaN
2     3.0     7.0     NaN
3     5.0     6.0     NaN
4     5.0     NaN     NaN
5     6.0     NaN     NaN
6     7.0     NaN     NaN
7     8.0     NaN     NaN
8     NaN     NaN     NaN
9     NaN     NaN     NaN
10    1.0     3.0     5.0
11    2.0     4.0     6.0
12    NaN     NaN     NaN
13    NaN     NaN     NaN
14    NaN     NaN     NaN
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks!both approaches from Sandeep and you work, but yours has a better performance 5ms against 9ms
2

Use reduce and DataFrame.combine_first:

from functools import reduce
df = reduce((lambda x, y: x.combine_first(y)), [df_main,df1,df2,df3,df4])

print(df)

    value  value2  value3
0     1.0     9.0     NaN
1     2.0     8.0     NaN
2     3.0     7.0     NaN
3     5.0     6.0     NaN
4     5.0     NaN     NaN
5     6.0     NaN     NaN
6     7.0     NaN     NaN
7     8.0     NaN     NaN
8     NaN     NaN     NaN
9     NaN     NaN     NaN
10    1.0     3.0     5.0
11    2.0     4.0     6.0
12    NaN     NaN     NaN
13    NaN     NaN     NaN
14    NaN     NaN     NaN

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.