2015-11-05 46 views
0

我是熊貓數據透視表的新手,如何在大熊貓數據透視表上獲得列值小於95的行數如何計算大熊貓數據透視表上每行的列值小於95

date     2015-09-26 2015-09-27 2015-09-28 2015-09-29 
bts_name                 
0001_Durgacomplex_NBSNL   94.45  98.99  93.24  92.3 
0002_Shivanagar_area_Bdr   100  100  94.45  100 
0003_Old_city_Bidar    98.99  94.45  100  100 
0004_Bidar_Mw_Station    94.45  98.99  100  100 
0005_Bidri_colony     100  94.45  100  98.99 

輸出應該是:

date     2015-09-26 2015-09-27 2015-09-28 2015-09-29  cnt 
bts_name                 
0001_Durgacomplex_NBSNL   94.45  98.99  93.24  92.3 3 
0002_Shivanagar_area_Bdr   100  100  94.45  100 1 
0003_Old_city_Bidar    98.99  94.45  100  100 1 
0004_Bidar_Mw_Station    94.45  98.99  100  100 1 
0005_Bidri_colony     100  94.45  100  98.99 1 

回答

0
In [28]: 
df['count'] = df.apply(lambda x : np.sum(x < 95) , axis = 1) 
df 
Out[28]: 
          2015-09-26 2015-09-27 2015-09-28 2015-09-29 count 
0001_Durgacomplex_NBSNL  94.45  98.99   93.24   92.30 3 
0002_Shivanagar_area_Bdr 100.00  100.00  94.45   100.00 1 
0003_Old_city_Bidar   98.99  94.45   100.00   100.00 1 
0004_Bidar_Mw_Station  94.45  98.99   100.00   100.00 1 
0005_Bidri_colony   100.00  94.45   100.00   98.99 1 
+0

謝謝你,是表示工作 –

1

只是比較整個DF,這將產生對整個陣列的布爾面膜,然後你可以sum行方向,這將轉換成True1False0

In [7]: 
df['count'] = (df < 95).sum(axis=1) 
df 

Out[7]: 
          2015-09-26 2015-09-27 2015-09-28 2015-09-29 \ 
0001_Durgacomplex_NBSNL  94.45  98.99  93.24  92.30 
0002_Shivanagar_area_Bdr  100.00  100.00  94.45  100.00 
0003_Old_city_Bidar   98.99  94.45  100.00  100.00 
0004_Bidar_Mw_Station   94.45  98.99  100.00  100.00 
0005_Bidri_colony    100.00  94.45  100.00  98.99 

          count 
0001_Durgacomplex_NBSNL  3 
0002_Shivanagar_area_Bdr  1 
0003_Old_city_Bidar   1 
0004_Bidar_Mw_Station   1 
0005_Bidri_colony    1 

這裏是布爾面具的樣子:

In [8]: 
df < 95 

Out[8]: 
         2015-09-26 2015-09-27 2015-09-28 2015-09-29 
0001_Durgacomplex_NBSNL  True  False  True  True 
0002_Shivanagar_area_Bdr  False  False  True  False 
0003_Old_city_Bidar   False  True  False  False 
0004_Bidar_Mw_Station   True  False  False  False 
0005_Bidri_colony    False  True  False  False