简体   繁体   中英

When extracting a Series from a DataFrame with multi-indexed columns I cannot read with read_csv

I have the following DataFrame:

>>>df
            A           B
            c1    c2    c1    c2
1.1.2000    10    11    10     9
2.1.2000    11     9     7    10
  .....

Now I want to save each column individually to a CSV file. (Don't ask why. I just have to.)

for column_name, series in df.iteritems():
    series.to_csv(filename, sep=";", header=True, date_format="%d.%m.%Y", decimal=',')

Note:

series.name ['A', 'c1'] etc. Always a list of strings instead of a string.

So far so good. Then I want to read the series again:

series = pd.read_csv(filename, index_col=0, sep=";", squeeze=True, decimal=',', parse_dates=True, dayfirst=True)

but I get:

>>>series
NaT                c1
NaT               NaN
2010-01-01         10
2010-01-02         11
   .....
Name: A

The headers are all mixed up. I suspect it is because of the MultiIndex. How can I write and read safely to/from a cvs file with multi-indexed columns?

You can use the header=[0, 1] command:

iterables = [['bar', 'baz', 'foo'], ['one', 'two']]
arrays = pd.MultiIndex.from_product(iterables, names=['first', 'second'])
index = pd.date_range(datetime(2015,1,1), periods=4)
df = pd.DataFrame(np.random.randn(4, 6), columns=arrays, index=index)

first            bar                 baz                 foo          
second           one       two       one       two       one       two
2015-01-01  0.668995 -2.938213 -0.829942 -0.450692  0.512050 -0.845328
2015-01-02  2.056507 -0.393159  0.682482 -1.298044  0.326979  0.281078
2015-01-03  0.651219 -1.455091  0.469010  0.309703  2.518411  0.054144
2015-01-04  0.905059  0.282328 -1.315723  0.306234 -1.640091 -1.962590

df.to_csv('multi.csv')
test = pd.read_csv('multi.csv', header=[0,1])

        first       bar                 baz                 foo          
       second       one       two       one       two       one       two
0  2015-01-01  0.668995 -2.938213 -0.829942 -0.450692  0.512050 -0.845328
1  2015-01-02  2.056507 -0.393159  0.682482 -1.298044  0.326979  0.281078
2  2015-01-03  0.651219 -1.455091  0.469010  0.309703  2.518411  0.054144
3  2015-01-04  0.905059  0.282328 -1.315723  0.306234 -1.640091 -1.962590

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM