Pandas allows to export dataframes into csv with to_csv('path/to/file.csv')
. However to me it is not clear how I can export (and import) a dataframe which uses MultiIndex
for rows and columns like e.g. this one from the corresponding advanced docs:
first bar baz foo
second one two one two one two
first second
bar one -0.410001 -0.078638 0.545952 -1.219217 -1.226825 0.769804
two -1.281247 -0.727707 -0.121306 -0.097883 0.695775 0.341734
baz one 0.959726 -1.110336 -0.619976 0.149748 -0.732339 0.687738
two 0.176444 0.403310 -0.154951 0.301624 -2.179861 -1.369849
foo one -0.954208 1.462696 -1.743161 -0.826591 -0.345352 1.314232
two 0.690579 0.995761 2.396780 0.014871 3.357427 -0.317441
To generate a dataframe like this with random data values in Jupyter notebook:
import pandas as pd
import numpy as np
column_index_matrix = [np.array(['bar', 'bar', 'baz', 'baz', 'foo', 'foo']),
np.array(['one', 'two', 'one', 'two', 'one', 'two'])]
column_names = ['first', 'second']
column_multiindex = pd.MultiIndex.from_arrays(column_index_matrix, names=column_names)
column_multiindex
row_multiindex = column_multiindex
df = pd.DataFrame(np.random.randn(6, 6), index=row_multiindex, columns=column_multiindex)
df
WHen running df.to_csv(r'df.csv', index=True)
the dataframe is exported into a csv file properly. However I don't know how to use pd.read_csv()
.