Skip to content

Instantly share code, notes, and snippets.

@phobson
Created November 25, 2014 22:01
Show Gist options
  • Save phobson/80a38c4d09b07d3efe17 to your computer and use it in GitHub Desktop.
Save phobson/80a38c4d09b07d3efe17 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