简体   繁体   中英

How to concatenate dataframes with different lengths (and samo columns)?

I am trying to stack multiple pandas Dataframes onto each other. They are of varying lengths, but all have 7 columns. I want to paste them below each other (concatenate over axis=0). So the outcome should be the sum of the individual lengths by 7. (x, 7).

To concatenate, I use:

import pandas as pd
import numpy as np

df_1 = pd.DataFrame(np.random.randint(0,100,size=(100, 7))
df_2 = pd.DataFrame(np.random.randint(0,100,size=(150, 7))
df_3 = pd.DataFrame(np.random.randint(0,100,size=(90, 7))
df_4 = pd.DataFrame(np.random.randint(0,100,size=(1001, 7))
df_5 = pd.DataFrame(np.random.randint(0,100,size=(1050, 7))
df_6 = pd.DataFrame(np.random.randint(0,100,size=(780, 7))
df_7 = pd.DataFrame(np.random.randint(0,100,size=(80, 7))

series = [df_1, df_2, df_3, df_4, df_5, df_6, df_7]

total = pd.concat(series, axis=0)

print(total.shape)

The outcome is (3251, 15), but the desired outcome is (3251, 7).

What should I do to get there?

Note, I did not specifically identify indexes or columns at the Dataframes. Maybe the solutions is somewhere there but I am not sure how to use those properties.

Ran your code and it works fine:

import pandas as pd
import numpy as np

df_1 = pd.DataFrame(np.random.randint(0,100,size=(100, 7)))
df_2 = pd.DataFrame(np.random.randint(0,100,size=(150, 7)))
df_3 = pd.DataFrame(np.random.randint(0,100,size=(90, 7)))
df_4 = pd.DataFrame(np.random.randint(0,100,size=(1001, 7)))
df_5 = pd.DataFrame(np.random.randint(0,100,size=(1050, 7)))
df_6 = pd.DataFrame(np.random.randint(0,100,size=(780, 7)))
df_7 = pd.DataFrame(np.random.randint(0,100,size=(80, 7)))

series = [df_1, df_2, df_3, df_4, df_5, df_6, df_7]

total = pd.concat(series, axis=0)

print(total)

     0   1   2   3   4   5   6
0   38  73   6  64  54  20  52
1   41  28   9  14  40  24  17
2   31  53   5  84  53   4  81
3   65   2  87  38  53  62  58
4   57  34   9  50  56  22  81
..  ..  ..  ..  ..  ..  ..  ..
75   1  63  71  71  76   3  95
76  87  47  33  43  79  99  73
77  12  58  11  93   5  35  18
78  16  41  44  15  79  74  73
79  76  54  34  83  35  22  50

[3251 rows x 7 columns]

I ran the code it's giving the desired output. The screenshot has been placed here:

代码的输出

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM