[英]When extracting a Series from a DataFrame with multi-indexed columns I cannot read with read_csv
I have the following DataFrame: 我有以下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. 现在,我想将每一列分别保存到CSV文件中。 (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.
series.name ['A','c1']等。始终是字符串列表,而不是字符串。
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.
我怀疑是因为MultiIndex。 How can I write and read safely to/from a cvs file with multi-indexed columns?
如何在具有多索引列的cvs文件中安全地读写文件?
You can use the header=[0, 1]
command: 您可以使用
header=[0, 1]
命令:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.