Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
262 views
in Technique[技术] by (71.8m points)

python - How to concatenate data frames with unequal number of rows and different column names

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

question from:https://stackoverflow.com/questions/65934592/how-to-concatenate-data-frames-with-unequal-number-of-rows-and-different-column

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...