Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用现有列作为索引_Python_Pandas_Indexing_Multi Index - Fatal编程技术网

Python 使用现有列作为索引

Python 使用现有列作为索引,python,pandas,indexing,multi-index,Python,Pandas,Indexing,Multi Index,如何让pandas通过数据框中已有的两列或多列对现有数据框进行索引 In [14]: df OUt [14]: date group colour count 0 2014-10-08 1 yellow 3 1 2014-10-08 2 yellow 6 3 2014-10-08 3 yellow 3 4 2014-10-08 1 blue 1 5 2014-10

如何让
pandas
通过数据框中已有的两列或多列对现有数据框进行索引

In [14]: df
OUt [14]:

  date         group    colour    count
0 2014-10-08   1        yellow    3
1 2014-10-08   2        yellow    6
3 2014-10-08   3        yellow    3
4 2014-10-08   1        blue      1
5 2014-10-08   2        blue      2
在这个例子中,我想通过
日期
颜色
对df进行索引

df = df.set_index(['date', 'group', 'colour'])
屈服

                         count
date       group colour       
2014-10-08 1     yellow      3
           2     yellow      6
           3     yellow      3
           1     blue        1
           2     blue        2
参考:

屈服

                         count
date       group colour       
2014-10-08 1     yellow      3
           2     yellow      6
           3     yellow      3
           1     blue        1
           2     blue        2
参考:

屈服

                         count
date       group colour       
2014-10-08 1     yellow      3
           2     yellow      6
           3     yellow      3
           1     blue        1
           2     blue        2
参考:

屈服

                         count
date       group colour       
2014-10-08 1     yellow      3
           2     yellow      6
           3     yellow      3
           1     blue        1
           2     blue        2
参考: