Home > Net >  Union of Index of Many Dataframes
Union of Index of Many Dataframes

Time:10-01

b.index = c1.index.union(c2.index).union(c3.index).union(c4.index).union(c5.index).union(c6.index).union(c7.index).union(c8.index).union(c9.index).union(c10.index)

I have dataframes c1 to c10 and want to create a new dataframe b that has the union index of all the dataframes. So far this is how I would do it, but I'm wondering if there's a more elegant solution.

How can I also define a function that allows me to take the index of a new dataframe c11 and add this on to b?

CodePudding user response:

Use:

dfs = [c1, c2, c3, ..., c10]

from functools import reduce

out = reduce(np.union1d, [x.index for x in dfs])
  • Related