根据内部级别值在数据框中选择行

时间:2015-07-02 21:00:03

标签: python pandas

尝试了各种方法,根据更深层次的索引选择数据框中的所有值。数据框如下所示:

enter image description here

我使用以下方法创建了数据框:

# create the time list
times = []
# change start and end dates to reduce processing during testing
start = datetime(2007, 2, 1, 0, 0, 0) #start = datetime(2007, 1, 1, 0, 0, 0)
end = datetime(2007, 2, 2, 23, 59, 59) #end = datetime(2007, 12, 31, 23, 59, 59)
for dt in rrule.rrule(rrule.MINUTELY, interval=10, dtstart=start, until=end):
    times.append(dt)

# create the solar zenith angle dataframe
columns = ['lat', 'lon', 'szen']
pos = ['left', 'centre', 'right']*len(times)

times3 = [x for pair in zip(times,times,times) for x in pair]
szen_df = pd.DataFrame(index=[times3, pos], columns=columns)
szen_df.index.names = ['time', 'pos']
szen_df.name = 'subsat_sol_zen'

我需要选择级别为' pos'的所有值。索引正在离开'离开' 我已经尝试了以下所有方法,但没有工作:

orb_df.loc(axis=0)[(slice(None),'left'),:]
orb_df.loc[(slice(None),'left'),:]
orb_df.loc[(slice(None),'left')]
orb_df.ix('left', level='pos', axis=1)
orb_df.loc[('left', level='pos')]
orb_df.loc(axis=0)[:, 'left')]

还有更多......

1 个答案:

答案 0 :(得分:0)

试试这个。 orb_df.xs('left', level='pos')

import pandas as pd
import numpy as np

np.random.seed(0)
multi_index = pd.MultiIndex.from_product([pd.date_range('2007-02-01 00:00:00', '2007-02-01 00:30:00', freq='10min'), ['left', 'center', 'right']], names=['time', 'pos'])

orb_df = pd.DataFrame(np.random.randn(12, 3), index=multi_index, columns=['lat', 'lon', 'szen'])

Out[178]: 
                               lat     lon    szen
time                pos                           
2007-02-01 00:00:00 left    1.7641  0.4002  0.9787
                    center  2.2409  1.8676 -0.9773
                    right   0.9501 -0.1514 -0.1032
2007-02-01 00:10:00 left    0.4106  0.1440  1.4543
                    center  0.7610  0.1217  0.4439
                    right   0.3337  1.4941 -0.2052
2007-02-01 00:20:00 left    0.3131 -0.8541 -2.5530
                    center  0.6536  0.8644 -0.7422
                    right   2.2698 -1.4544  0.0458
2007-02-01 00:30:00 left   -0.1872  1.5328  1.4694
                    center  0.1549  0.3782 -0.8878
                    right  -1.9808 -0.3479  0.1563

orb_df.xs('left', level='pos')

Out[179]: 
                        lat     lon    szen
time                                       
2007-02-01 00:00:00  1.7641  0.4002  0.9787
2007-02-01 00:10:00  0.4106  0.1440  1.4543
2007-02-01 00:20:00  0.3131 -0.8541 -2.5530
2007-02-01 00:30:00 -0.1872  1.5328  1.4694

使用您发送给我的csv文件,

import pandas as pd
import numpy as np

file_path = '/home/Jian/Downloads/test.csv'
orb_df = pd.read_csv(file_path, index_col=[0,1])

 Out[10]: 
                                 lat       lon      szen
 time                pos                                
 2007-02-01 00:00:00 left    20.3429  -31.4709  146.9311
                     centre  20.5866  -40.6835  138.3134
                     right   20.3429  -49.8961  129.6912
 2007-02-01 00:10:00 left    54.5340  -37.7192  132.4366
                     centre  55.4665  -52.7132  124.4594
                     right   54.5340  -67.7072  116.3623
 2007-02-01 00:20:00 left    77.6419 -100.0341  105.0689
                     centre  81.1240 -144.5732  100.1134
                     right   77.6419 -189.1123   95.0044
 2007-02-01 00:30:00 left    51.0248  160.1143   69.6712
                     centre  51.8437  146.3068   73.7495
                     right   51.0248  132.4994   78.1162
 2007-02-01 00:40:00 left    16.6625  144.2700   44.4155
                     centre  16.8591  135.2550   50.7311
                     right   16.6625  126.2400   57.5004
 ...                             ...       ...       ...
 2007-02-01 23:10:00 left   -75.6124   60.5806   83.4491
                     centre -78.4466   23.4148   84.4668
                     right  -75.6124  -13.7510   85.6084
 2007-02-01 23:20:00 left   -45.6521   -5.7924  114.9838
                     centre -46.3280  -18.1932  110.5555
                     right  -45.6521  -30.5941  105.7779
 2007-02-01 23:30:00 left   -11.1859  -19.3416  138.9183
                     centre -11.3158  -28.1436  132.1387
                     right  -11.1859  -36.9456  124.9512
 2007-02-01 23:40:00 left    23.6844  -26.8516  146.3563
                     centre  23.9728  -36.2861  137.7239
                     right   23.6844  -45.7205  129.0911
 2007-02-01 23:50:00 left    57.6906  -33.1493  130.0771
                     centre  58.7443  -49.4604  122.1213
                     right   57.6906  -65.7715  114.0516

 [432 rows x 3 columns]

orb_df.xs('left', level='pos')

 Out[12]: 
                          lat       lon      szen
 time                                            
 2007-02-01 00:00:00  20.3429  -31.4709  146.9311
 2007-02-01 00:10:00  54.5340  -37.7192  132.4366
 2007-02-01 00:20:00  77.6419 -100.0341  105.0689
 2007-02-01 00:30:00  51.0248  160.1143   69.6712
 2007-02-01 00:40:00  16.6625  144.2700   44.4155
 2007-02-01 00:50:00 -18.2235  136.2792   32.9172
 2007-02-01 01:00:00 -52.4325  130.0414   46.2247
 2007-02-01 01:10:00 -77.7702   83.6864   71.4977
 2007-02-01 01:20:00 -53.4182  -30.5365  108.3205
 2007-02-01 01:30:00 -19.2653  -47.6486  133.9691
 2007-02-01 01:40:00  15.6083  -55.9040  147.1195
 2007-02-01 01:50:00  50.0034  -62.1749  135.2742
 2007-02-01 02:00:00  77.4362  -94.9213  111.4778
 2007-02-01 02:10:00  55.5404  138.8276   73.4941
 2007-02-01 02:20:00  21.3963  120.3563   47.3988
 ...                      ...       ...       ...
 2007-02-01 21:30:00 -72.6257   64.6434   88.9781
 2007-02-01 21:40:00 -41.0389   16.8782  118.6125
 2007-02-01 21:50:00  -6.4433    4.7938  141.3265
 2007-02-01 22:00:00  28.3995   -2.3735  145.2392
 2007-02-01 22:10:00  62.0610   -8.8964  127.0542
 2007-02-01 22:20:00  74.2087 -118.5913   93.6189
 2007-02-01 22:30:00  43.1833  185.4045   63.0598
 2007-02-01 22:40:00   8.5652  172.6704   39.7115
 2007-02-01 22:50:00 -26.2772  165.3590   34.1985
 2007-02-01 23:00:00 -60.0139  158.9824   51.5113
 2007-02-01 23:10:00 -75.6124   60.5806   83.4491
 2007-02-01 23:20:00 -45.6521   -5.7924  114.9838
 2007-02-01 23:30:00 -11.1859  -19.3416  138.9183
 2007-02-01 23:40:00  23.6844  -26.8516  146.3563
 2007-02-01 23:50:00  57.6906  -33.1493  130.0771

 [144 rows x 3 columns]