为列提供多个索引/标头

时间:2015-09-03 08:16:15

标签: python pandas multi-index

我正在使用基本上是时间序列的pandas数据帧:

             level
Date              
1976-01-01  409.67
1976-02-01  409.58
1976-03-01  409.66
…

我想拥有的是level列的多个索引/标题,如下所示:

           Station1                   #Name of the datasource
           43.1977317,-4.6473648,5    #Lat/Lon of the source
           Precip                     #Type of data
Date              
1976-01-01  409.67
1976-02-01  409.58
1976-03-01  409.66
…

基本上我正在搜索Mydata.columns.level1 = ['Station1']Mydata.columns.level2 = [Lat,Lon]Mydata.columns.level3 = ['Precip']等内容。

原因是单个位置可以有多个数据集,并且我希望能够从后续合并的大数据帧中从一个位置选择所有数据,或者从所有位置选择特定类型的所有数据。< / p>

我可以从pandas文档中设置一个示例数据框,并测试我的选择,但是对于我的真实数据,我需要一种不同的方式来设置索引,如示例所示。

示例:

构建一个小型数据框

header = [np.array(['location','location','location','location2','location2','location2']), 
np.array(['S1','S2','S3','S1','S2','S3'])] 
df = pd.DataFrame(np.random.randn(5, 6), index=['a','b','c','d','e'], columns = header )   

df
    location                      location2                    
         S1        S2        S3         S1        S2        S3
a -1.469932 -1.544511 -1.373463  -0.317262  0.024832 -0.641000
b  0.047170 -0.339423  1.351253   0.601172 -1.607339  0.035932
c -0.257479  1.140829  0.188291  -0.242490  1.019315 -1.163429
d  0.832949  0.098170 -0.818513  -0.070383  0.557419 -0.489839
e -0.628549 -0.158419  0.366167  -2.319316 -0.474897 -0.319549

选择数据类型或位置:

df.loc(axis=1)[:,'S1']

   location  location2
         S1         S1
a -1.469932  -0.317262
b  0.047170   0.601172
c -0.257479  -0.242490
d  0.832949  -0.070383
e -0.628549  -2.319316

df['location']

         S1        S2        S3
a -1.469932 -1.544511 -1.373463
b  0.047170 -0.339423  1.351253
c -0.257479  1.140829  0.188291
d  0.832949  0.098170 -0.818513
e -0.628549 -0.158419  0.366167

或者我只是在寻找错误的术语?因为文档中所有示例的90%,以及这里的问题仅将垂直“东西”(在我的情况下为日期或abcde)视为索引,而对我的测试数据的快速df.index.values也只是让我垂直array(['a', 'b', 'c', 'd', 'e'], dtype=object)

1 个答案:

答案 0 :(得分:12)

您可以使用multiIndex为多个列提供每个级别的名称。使用MultiIndex.from_product()从多个迭代的笛卡尔积中生成multiIndex。

header = pd.MultiIndex.from_product([['location1','location2'],
                                     ['S1','S2','S3']],
                                    names=['loc','S'])
df = pd.DataFrame(np.random.randn(5, 6), 
                  index=['a','b','c','d','e'], 
                  columns=header)

两个级别是loc和S.

df
loc location1                     location2                    
S          S1        S2        S3        S1        S2        S3
a   -1.245988  0.858071 -1.433669  0.105300 -0.630531 -0.148113
b    1.132016  0.318813  0.949564 -0.349722 -0.904325  0.443206
c   -0.017991  0.032925  0.274248  0.326454 -0.108982  0.567472
d    2.363533 -1.676141  0.562893  0.967338 -1.071719 -0.321113
e    1.921324  0.110705  0.023244 -0.432196  0.172972 -0.50368

现在,您可以使用xs根据级别对日期框进行切片。

df.xs('location1',level='loc',axis=1)

S        S1        S2        S3
a -1.245988  0.858071 -1.433669
b  1.132016  0.318813  0.949564
c -0.017991  0.032925  0.274248
d  2.363533 -1.676141  0.562893
e  1.921324  0.110705  0.02324

df.xs('S1',level='S',axis=1)

loc  location1  location2
a    -1.245988   0.105300
b     1.132016  -0.349722
c    -0.017991   0.326454
d     2.363533   0.967338
e     1.921324  -0.43219