迭代组并输出满足 .loc 条件的那些组
Iterate over groups and output those groups which met .loc conditions
- 我正在尝试遍历组(groupedby
AC No
)
- 满足给定条件(有12行)的组作为输出。
data.loc[(data['Position'] <= 3) & (data['Votes %'] > 10.0) ].shape[0]) == 12
被分配一个虚拟输出为 1.
让我们从新鲜简单的开始
我已将新过滤的数据集存储为
q5_data = data.loc[(data['Position'] <= 3) & (data['Votes %'] > 10.0)]
print(q5_data.head(10))
过滤后的数据集
Election Year Position Name Votes Votes % \
0 2010-01-01 1 Rajesh Singh 42289 29.4
1 2010-01-01 2 Mukesh Kumar Kushwaha 27618 19.2
2 2010-01-01 3 Dheerendra Pratap Singh 20886 14.5
14 2010-01-01 1 Bhagirathi Devi 51993 41.5
15 2010-01-01 2 Naresh Ram 22211 17.7
16 2010-01-01 3 Subodh Kumar 20095 16.0
31 2010-01-01 1 Satish Chandra Dubey 45022 38.1
32 2010-01-01 2 Alok Prasad Verma 24794 21.0
33 2010-01-01 3 Fakhruddin Khan 22381 18.9
46 2010-01-01 1 Prabhat Ranjan Singh 67510 50.4
Party AC name AC No
0 Janata Dal (United) Valmiki Nagar 1
1 Rashtriya Janata Dal Valmiki Nagar 1
2 Bahujan Samaj Party Valmiki Nagar 1
14 Bharatiya Janta Party Ramnagar 2
15 Indian National Congress Ramnagar 2
16 Nationalist Congress Party Ramnagar 2
31 Bharatiya Janta Party Narkatiaganj 3
32 Indian National Congress Narkatiaganj 3
33 Independent Narkatiaganj 3
46 Janata Dal (United) Bagaha 4
在上面的数据集中,我只想输出那些总计数为 12 的组或 AC Name
。
可以通过掩码GroupBy.sum
统计匹配值,然后过滤:
m = (data['Position'] <= 3) & (data['Votes %'] > 10.0)
s = data.assign(test = m).groupby('AC No')['test'].sum()
out = s.index[s == 12].tolist()
- 我正在尝试遍历组(groupedby
AC No
) - 满足给定条件(有12行)的组作为输出。
data.loc[(data['Position'] <= 3) & (data['Votes %'] > 10.0) ].shape[0]) == 12
被分配一个虚拟输出为 1.
让我们从新鲜简单的开始 我已将新过滤的数据集存储为
q5_data = data.loc[(data['Position'] <= 3) & (data['Votes %'] > 10.0)]
print(q5_data.head(10))
过滤后的数据集
Election Year Position Name Votes Votes % \
0 2010-01-01 1 Rajesh Singh 42289 29.4
1 2010-01-01 2 Mukesh Kumar Kushwaha 27618 19.2
2 2010-01-01 3 Dheerendra Pratap Singh 20886 14.5
14 2010-01-01 1 Bhagirathi Devi 51993 41.5
15 2010-01-01 2 Naresh Ram 22211 17.7
16 2010-01-01 3 Subodh Kumar 20095 16.0
31 2010-01-01 1 Satish Chandra Dubey 45022 38.1
32 2010-01-01 2 Alok Prasad Verma 24794 21.0
33 2010-01-01 3 Fakhruddin Khan 22381 18.9
46 2010-01-01 1 Prabhat Ranjan Singh 67510 50.4
Party AC name AC No
0 Janata Dal (United) Valmiki Nagar 1
1 Rashtriya Janata Dal Valmiki Nagar 1
2 Bahujan Samaj Party Valmiki Nagar 1
14 Bharatiya Janta Party Ramnagar 2
15 Indian National Congress Ramnagar 2
16 Nationalist Congress Party Ramnagar 2
31 Bharatiya Janta Party Narkatiaganj 3
32 Indian National Congress Narkatiaganj 3
33 Independent Narkatiaganj 3
46 Janata Dal (United) Bagaha 4
在上面的数据集中,我只想输出那些总计数为 12 的组或 AC Name
。
可以通过掩码GroupBy.sum
统计匹配值,然后过滤:
m = (data['Position'] <= 3) & (data['Votes %'] > 10.0)
s = data.assign(test = m).groupby('AC No')['test'].sum()
out = s.index[s == 12].tolist()