Getting sub-dataframe after sorting and groupby

2024/10/6 18:29:30

I have a dataframe dfas:

  Election Year     Votes   Vote %      Party              Region   
0   2000            42289   29.40   Janata Dal (United)     A
1   2000            27618   19.20   Rashtriya Janata Dal    A
2   2000            20886   14.50   Bahujan Samaj Party     B 
3   2000            17747   12.40   Congress                B
4   2000            14047   19.80   Independent             C
5   2000            17047   10.80   JLS                     C
6   2005            8358    15.80   Janvadi Party           A
7   2005            4428    13.10   Independent             A
8   2005            1647    1.20    Independent             B
9   2005            1610    11.10   Independent             B
10  2005            1334    15.06   Nationalist             C
11  2005            1834    18.06   NJM                     C
12  2010            21114   20.80   Independent             A
13  2010            1042    10.5    Bharatiya Janta Dal     A
14  2010            835     0.60    Independent             B
15  2010            14305   15.50   Independent             B
16  2010            22211   17.70   Congress                C
16  2010            20011   14.70   INC                     C

How can I get the list of the regions that have two or more parties getting more than vote % greater than 10 every election year?

Desired output:

Election Year    Region    Vote %2000             A        29.402000             A        19.402000             C        19.802000             C        10.802005             A        15.802005             A        13.102005             C        15.062005             C        18.062010             A        20.802010             A        10.52010             C        17.702010             C        14.70

Output contains only regions having more than 10% vote every year and Election year and region name in sorted in ascending order. So, here only Region "A" and "C" will be there in the output.

I have used the following code to sort "Vote %" in descending order after grouping by "Election year" and "Region" and to then compare the top 2 Vote% every year, but it is giving an error.

df1 = df.groupby(['Election Year','Region'])sort_values('Vote %', ascending = False).reset_index()
Answer

Try with groupby filter:

cols = ['Election Year', 'Region', 'Vote %']
df1 = (df.groupby('Region').filter(lambda g: g['Vote %'].ge(10).all()).sort_values(cols, ascending=(True, True, False))[cols].reset_index(drop=True)
)

df1:

    Election Year Region  Vote %
0            2000      A   29.40
1            2000      A   19.20
2            2000      C   19.80
3            2000      C   10.80
4            2005      A   15.80
5            2005      A   13.10
6            2005      C   18.06
7            2005      C   15.06
8            2010      A   20.80
9            2010      A   10.50
10           2010      C   17.70
11           2010      C   14.70

df used:

df = pd.DataFrame({'Election Year': [2000, 2000, 2000, 2000, 2000, 2000, 2005, 2005, 2005,2005, 2005, 2005, 2010, 2010, 2010, 2010, 2010, 2010],'Votes': [42289, 27618, 20886, 17747, 14047, 17047, 8358, 4428, 1647, 1610,1334, 1834, 21114, 1042, 835, 14305, 22211, 20011],'Vote %': [29.4, 19.2, 14.5, 12.4, 19.8, 10.8, 15.8, 13.1, 1.2, 11.1, 15.06,18.06, 20.8, 10.5, 0.6, 15.5, 17.7, 14.7],'Party': ['Janata Dal (United)', 'Rashtriya Janata Dal','Bahujan Samaj Party', 'Congress', 'Independent', 'JLS','Janvadi Party', 'Independent', 'Independent', 'Independent','Nationalist', 'NJM', 'Independent', 'Bharatiya Janta Dal','Independent', 'Independent', 'Congress', 'INC'],'Region': ['A', 'A', 'B', 'B', 'C', 'C', 'A', 'A', 'B', 'B', 'C', 'C', 'A','A', 'B', 'B', 'C', 'C']
})
https://en.xdnf.cn/q/118920.html

Related Q&A

Use .vss stencil file to generate shapes by python code (use .vdx?)

I want to have my python program generate visio drawings using shapes from a stencil (.vss) file. How can I do that? I think I could generate the xml formatted .vdx file, but there isnt a lot of docum…

How can I capture detected image of object Yolov3 and display in flask

I am working on Real Time Object Detection using YOLOv3 with OpenCV and Python. Its works well. Currently I try to capture detected image of object and display in flask. Do someone know how to implemen…

ValueError: Shapes (2, 1) and () are incompatible

I have to use Tensorflow 0.11 for this code repo and this is the error I get:(py35) E:\opensource_codes\gesture_recognition\hand3d-master>python run.py Traceback (most recent call last):File "r…

Subtotals for Pandas pivot table index and column

Id like to add subtotal rows for index #1 (ie. Fruits and Animal) and subtotal columns for columns (ie. 2015 and 2016).For the subtotal columns, I could do something like this, but it seems inefficient…

Create two new columns derived from original columns in Python

I have a dataframe, df, that contains two columns that contain quarter values. I would like to create two more columns that are the equivalent "long dates". Data ID Quarter Delivery A …

In dataframe: how to pull minutes and seconds combinedly(mm:ss) from timedelta using python

i have already tried these but by this we can only get hour/minute/second but not both minute and second In[7]: df[B] = df[A].dt.components[hours] df Out[7]:A B0 02:00:00 2 1 01:00:00 1from this tim…

Understanding python numpy syntax to mask and filter array

Please help me understand how the lines below are working.How does a pair of parentheses create an array and then individual elements go through logical condition check and create a new array? How doe…

How to get the proper link from a website using python beautifulsoup?

When I try to scrape roster links, I get https://gwsports.com/roster.aspx?path=wpolo when I open it on chrome it changes to https://gwsports.com/sports/mens-water-polo/roster. I want to scrape it in p…

tkinter frame propagate not behaving?

If you uncomment the options_frame_title you will see that it does not behave properly. Am I missing something? That section was just copied and pasted from the preview_frame_title and that seems to h…

python modules installing Error Visual c++ 14.0 is required [duplicate]

This question already has answers here:pip install ecos errors with "Microsoft Visual C++ 14.0 is required." [duplicate](1 answer)Error "Microsoft Visual C++ 14.0 is required (Unable to …