Skip to content

Instantly share code, notes, and snippets.

@csharpforevermore
Forked from phobson/pdconcat.py
Created June 16, 2018 15:07
Show Gist options
  • Save csharpforevermore/ddd7e971630c076e72a79813fd182792 to your computer and use it in GitHub Desktop.
Save csharpforevermore/ddd7e971630c076e72a79813fd182792 to your computer and use it in GitHub Desktop.
funky pandas concat
import numpy
import pandas
numpy.random.seed(0)
index1 = pandas.MultiIndex.from_product(
[['A'], ['b'], ['one', 'two']],
names=['City', 'Street', 'House']
)
index2 = pandas.MultiIndex.from_product(
[['C'], ['three', 'four'], ['d']],
names=['City', 'House', 'Street']
)
df1 = pandas.DataFrame(
[['one-c1', 'one-c2'], ['two-c1', 'two-c2']],
index=index1,
columns=['c1', 'c2']
)
print(df1)
df2 = pandas.DataFrame(
[['three-c2', 'three-c1'], ['four-c2', 'four-c1']],
index=index2,
columns=['c2', 'c1']
)
print(df2)
print(pandas.concat([df1, df2]))
### the work-around
index_names = df1.index.names
df12 = pandas.concat([df1.reset_index(), df2.reset_index()]).set_index(index_names)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment