3

I am trying to concatenate four data frames with an unequal number of rows and different column names. Here I am placing my code and output what I am getting.

import pandas as pd data1 = pd.DataFrame({'Col':[33,44,55,67], 'Col1':[44,55,55,555]}) data2 = pd.DataFrame({'Col2':[33,44], 'Col3':[22,22]}) data3 = pd.DataFrame({'Col4':[33,44,44], 'Col5':[22,22,44]}) data4 = pd.DataFrame({'Col6':[33], 'Col7':[22], 'Col8':[44]}) data5 = pd.concat([data1, data2, data3, data4]) data5 

here is my actual output

enter image description here

what I am expecting is:

enter image description here

Thanks in advance

2
  • What is the output size you d expect if different number of rows given? Max of them all? Commented Jan 28, 2021 at 9:50
  • Could you also please describe what have you tried to solve? Commented Jan 28, 2021 at 9:51

1 Answer 1

3

You can use concat with axis=1, but first repeat values for match maximal lengths of rows:

dfs = [data1, data2, data3, data4] maxlen = len(max(dfs, key=len)) print (maxlen) 4 dfs = [pd.concat([x] * int(np.ceil(maxlen / len(x))), ignore_index=True).iloc[:maxlen] for x in dfs] data5 = pd.concat(dfs, axis=1) print (data5) Col Col1 Col2 Col3 Col4 Col5 Col6 Col7 Col8 0 33 44 33 22 33 22 33 22 44 1 44 55 44 22 44 22 33 22 44 2 55 55 33 22 44 44 33 22 44 3 67 555 44 22 33 22 33 22 44 
Sign up to request clarification or add additional context in comments.

7 Comments

i am getting error 'str' object has no attribute concate. because in my original dataframe there is text in rows. how to overcome this error
@SuneelKumar - hmm, I think problem is not text, try change sample data and working well.
@SuneelKumar - Are in dfs list DataFrames ?
yeah it's working fine. It was my mistake because i wrote p.concat instead of pd.concat
@SuneelKumar - Super!
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.