Reputation: 705
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
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
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
Reputation: 11
df = df.sort_values(by=['c2'],ascending = True)
split_len = int(0.8*len(df))
df = df.iloc[split_len:]
Upvotes: 1