从csv文件中的列上读取多索引 [英] Read multi-index on the columns from csv file

查看:74
本文介绍了从csv文件中的列上读取多索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.csv文件,如下所示:

I have a .csv file that looks like this:

Male, Male, Male, Female, Female
R, R, L, R, R
.86, .67, .88, .78, .81

我想将其读入df,这样我就可以了:

I want to read that into a df, so that I have:

    Male        Female
    R       L   R
0   .86 .67 .88 .78 .81

我做到了:

df = pd.read_csv('file.csv', header=[0,1])

但是headers不会剪切它.导致

Empty DataFrame
Columns: [(Male, R), (Male, R), (Male, L), (Female, R), (Female, R)]
Index: []

但是,标头上的文档说:

Yet, the docs on headers says:

(...)Can be a list of integers that specify row
locations for a multi-index on the columns E.g. [0,1,3]

我做错了什么?我怎样才能使它正常工作?

What am I doing wrong? How can I possibly make it work?

推荐答案

我认为问题在于您有重复的列:两列(女,R).

I think the problem is that you have duplicated columns: two ( Female, R).

不确定是错误还是重复的列不可接受.这是适合您的解决方法:

Not sure whether it's a bug or the duplicated columns are unacceptable. Here's a workaround for you:

In [61]: df = pd.read_csv('test.csv', header=[0, 1], skipinitialspace=True, tupleize_cols=True)

In [62]: df
Out[62]: 
   (Male, R)  (Male, R)  (Male, L)  (Female, R)  (Female, R)
0       0.67       0.67       0.88         0.81         0.81

[1 rows x 5 columns]

然后将列的类型从Index转换为MultiIndex

In [63]: df.columns = pd.MultiIndex.from_tuples(df.columns)

In [64]: df
Out[64]: 
   Male              Female      
      R     R     L       R     R
0  0.67  0.67  0.88    0.81  0.81

[1 rows x 5 columns]

这篇关于从csv文件中的列上读取多索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆