freefrog
freefrog

Reputation: 705

How to create a pandas dataframe according to the top 20% value in a column?

There is a pandas data frame:

df = pd.DataFrame({'c1':['a','b','c','d','e','f','g','h','i','j'],
                   'c2':[10,12,23,4,18,98,11,23,33,99]})


    c1  c2
0   a   10
1   b   12
2   c   23
3   d   4
4   e   18
5   f   98
6   g   11
7   h   23
8   i   33
9   j   99

I want to create a new data frame that only contains the top 20% rows according to values in column c2, in this case:

output:

   c1   c2
0   f   98
1   j   99

Upvotes: 3

Views: 2094

Answers (4)

piRSquared
piRSquared

Reputation: 294546

Using the pct=True option on the pd.Series.rank method

df[df.c2.rank(pct=True).gt(.8)]

  c1  c2
5  f  98
9  j  99

Upvotes: 1

Alexander
Alexander

Reputation: 109736

In the interest of variety...

top_percentage = 0.2
>>> df.sort_values('c2').tail(int(len(df) * top_percentage))
# Output:
#    c1  c2
# 5  f  98
# 9  j  99

Upvotes: 2

Bhushan Mehta
Bhushan Mehta

Reputation: 11

df = df.sort_values(by=['c2'],ascending = True)
split_len = int(0.8*len(df))
df = df.iloc[split_len:]

Upvotes: 1

akuiper
akuiper

Reputation: 215137

You could use quantile method to calculate the 80 percentile threshold and keep values larger than it:

df[df.c2.gt(df.c2.quantile(0.8))]

#  c1   c2
#5  f   98
#9  j   99

Or use nlargest:

df.nlargest(int(len(df) * 0.2), 'c2')
#  c1   c2
#9  j   99
#5  f   98

Upvotes: 2

Related Questions