Skip to content Skip to sidebar Skip to footer

Getting Sub-dataframe After Sorting And Groupby

I have a dataframe dfas: Election Year Votes Vote % Party Region 0 2000 42289 29.40 Janata Dal (United) A 1 2000 2761

Solution 1:

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']
})

Post a Comment for "Getting Sub-dataframe After Sorting And Groupby"