我是pandas数据透视表的新手,如何获取pandas数据透视表中一行的列值小于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 :(得分:1)
只需比较整个df,这会产生一个针对整个数组的布尔掩码,然后你可以sum
逐行,这会将True
转换为1
和{{1转到False
:
0
这是布尔掩码的样子:
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
答案 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