Zarrie
Zarrie

Reputation: 333

How to concat dataframes with missing indices

I have a list of multicolumn dataframes like A idx b c 0 0.1 0.5 1 0.2 0.2 2 0.3 0.4 3 . . . . . . . . . 0.9 0.1 2 1 0.5 6

I want to concatenate them in one DF like: A B idx b c e f 0 ...... 0.1 0.5 1 ...... 0.2 0.2 2 0.3 0.4 3 . . . . . . . . . 0.9 0.1 2 1 0.5 6 ...... using only the index column one and appending all the dataframes next to the first one. Unfortunately some of the dataframe are missing some of the indices e.g. 0.1, 0.2, {missing 0.3}, 0.4, ... 1

I tried using df = pd.concat(dfs, axis=0) but what I get is df with 80 rows ( as much as I expect, because I have a list of 40 2-column dataframes ) but another 400 rows because it adds every df as row also. How to deal with that?

Upvotes: 1

Views: 1578

Answers (1)

Joe
Joe

Reputation: 12417

If I understood you correctly, you could try in this way:

index = df.index
df.reset_index(drop=True, inplace=True)
dfs.reset_index(drop=True, inplace=True)
df = df.join(dfs)
df = df.set_index(index)

Upvotes: 1

Related Questions