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:

ElectionYearRegionVote%02000      A29.4012000      A19.2022000      C19.8032000      C10.8042005      A15.8052005      A13.1062005      C18.0672005      C15.0682010      A20.8092010      A10.50102010      C17.70112010      C14.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"