将已处理的CSV文件导入Pandas Dataframe

时间:2018-07-20 17:33:08

标签: python-3.x pandas

我已获得以下CSV文件,可将其导入Pandas DataFrame

,,,,Facet,Facet,Facet,Facet,Value,Value,Value,Value
Snipit,,,,0,0,0,0,0,0,0,0
Grainy,,,,0,0,1,2,0,0,1,2
Arroyo,Position,Chunk,Grade,,,,,,,,
0,0,0,5,2.0,2.0,2.0,0.0,2.0,2.0,2.0,1.2
0,0,0,21,0.0,0.0,0.0,2.0,1.0,1.0,1.0,2.0

该表由Arroyo,Position,Chunk,Grade和columns进行了多索引 没有,“ Snipit”,“ Grainy”,“ Tiny”已保存到CSV文件。

当尝试使用pd.read_csv时,我得到以下信息:

Unnamed: 0  Unnamed: 1  Unnamed: 2  Unnamed: 3  Facet   Facet.1     Facet.2     Facet.3     Value   Value.1 Value.2 Value.3
0   Snipit              NaN         NaN        NaN  0.0     0.0         0.0         0.0         0.0     0.0     0.0     0.0
1   Grainy              NaN         NaN        NaN  0.0     0.0         1.0         2.0         0.0     0.0     1.0     2.0
2   Tiny                NaN         NaN        NaN  0.0     1.0         0.0         2.0         0.0     1.0     0.0     2.0
3   Arroyo      Position    Chunk       Grade       NaN     NaN         NaN         NaN         NaN     NaN     NaN     NaN
4   0                   0       0       5           2.0     2.0         2.0         0.0         2.0     2.0     2.0     1.2
5   0                   0       0       21          0.0     0.0         0.0         2.0         1.0     1.0     1.0     2.0

所需的输出

                                    Facet   Facet   Facet   Facet   Value   Value   Value   Value
Snipit                              0       0       0       0       0       0       0       0
Grainy                              0       0       1       2       0       0       1       2
Tiny                                0       1       0       2       0       1       0       2
Arroyo  Position    Chunk   Grade
0       0           0       5       2.0     2.0     2.0     0.0     2.0     2.0     2.0     1.2
0       0           0       21      0.0     0.0     0.0     2.0     1.0     1.0     1.0     2.0

我知道我需要定义索引和列,但是只有我可以找到使用平面文件的地方了。就我而言,我正在处理已处理的数据,找不到该示例。

我可以将这样的文件导入pandas DataFrame中吗?从阅读的内容中我什么都看不到

谢谢。

1 个答案:

答案 0 :(得分:0)

您可以在index_col中使用read_csv()参数设置多索引:

df = pd.read_csv('data.csv', index_col=[0,1,2,3])