'How to print top 10 elements of a dataframe column
mask = df.groupby('prod_title')['trans_quantity'].sum().reset_index()
mask = mask.sort_values(by='trans_quantity',ascending=False)
top_num_sales = mask['prod_title'].head(10)
I want to print only the names from the top 10 elements from prod_title
I tried df['prod_title'] but it returns the index also I only want the names of the elements
df.head(20) data below
`
{'trans_id': {0: 10300097,
1: 10300093,
2: 10300093,
3: 10300093,
4: 10300093,
5: 10300091,
6: 10300096,
7: 10300103,
8: 10300100,
9: 10300102,
10: 10300101,
11: 10300101,
12: 10300098,
13: 10300094,
14: 10300099,
15: 10300092,
16: 10300095,
17: 10300107,
18: 10300113,
19: 10300113},
'prod_upc': {0: 719638485153,
1: 73201504044,
2: 719638485153,
3: 441530839394,
4: 733426809698,
5: 344934101144,
6: 717036112695,
7: 242313721729,
8: 100469015054,
9: 287663658863,
10: 441530839394,
11: 287663658863,
12: 242313721729,
13: 374613020864,
14: 832878954342,
15: 469757173540,
16: 73201504044,
17: 374613020864,
18: 100469015054,
19: 469757173540},
'cust_id': {0: 1001019,
1: 1001015,
2: 1001015,
3: 1001015,
4: 1001015,
5: 1001012,
6: 1001018,
7: 1001025,
8: 1001022,
9: 1001024,
10: 1001023,
11: 1001023,
12: 1001020,
13: 1001016,
14: 1001021,
15: 1001014,
16: 1001017,
17: 1001028,
18: 1001034,
19: 1001034},
'trans_timestamp': {0: Timestamp('2021-01-01 07:35:21.439873'),
1: Timestamp('2021-01-01 09:33:37.499660'),
2: Timestamp('2021-01-01 09:33:37.499660'),
3: Timestamp('2021-01-01 09:33:37.499660'),
4: Timestamp('2021-01-01 09:33:37.499660'),
5: Timestamp('2021-01-01 10:08:32.241027'),
6: Timestamp('2021-01-01 10:43:40.372766'),
7: Timestamp('2021-01-01 11:05:32.193258'),
8: Timestamp('2021-01-01 11:39:46.749952'),
9: Timestamp('2021-01-01 11:44:27.467186'),
10: Timestamp('2021-01-01 12:14:47.132247'),
11: Timestamp('2021-01-01 12:14:47.132247'),
12: Timestamp('2021-01-01 12:21:14.473565'),
13: Timestamp('2021-01-01 12:40:29.169799'),
14: Timestamp('2021-01-01 12:52:02.370864'),
15: Timestamp('2021-01-01 13:10:33.543618'),
16: Timestamp('2021-01-01 14:32:28.903947'),
17: Timestamp('2021-01-02 03:58:22.059670'),
18: Timestamp('2021-01-02 06:14:35.006901'),
19: Timestamp('2021-01-02 06:14:35.006901')},
'trans_year': {0: 2021,
1: 2021,
2: 2021,
3: 2021,
4: 2021,
5: 2021,
6: 2021,
7: 2021,
8: 2021,
9: 2021,
10: 2021,
11: 2021,
12: 2021,
13: 2021,
14: 2021,
15: 2021,
16: 2021,
17: 2021,
18: 2021,
19: 2021},
'trans_month': {0: 1,
1: 1,
2: 1,
3: 1,
4: 1,
5: 1,
6: 1,
7: 1,
8: 1,
9: 1,
10: 1,
11: 1,
12: 1,
13: 1,
14: 1,
15: 1,
16: 1,
17: 1,
18: 1,
19: 1},
'trans_day': {0: 1,
1: 1,
2: 1,
3: 1,
4: 1,
5: 1,
6: 1,
7: 1,
8: 1,
9: 1,
10: 1,
11: 1,
12: 1,
13: 1,
14: 1,
15: 1,
16: 1,
17: 2,
18: 2,
19: 2},
'trans_hour': {0: 1,
1: 1,
2: 1,
3: 1,
4: 1,
5: 1,
6: 1,
7: 1,
8: 1,
9: 1,
10: 1,
11: 1,
12: 1,
13: 1,
14: 1,
15: 1,
16: 1,
17: 2,
18: 2,
19: 2},
'trans_quantity': {0: 1,
1: 1,
2: 1,
3: 2,
4: 1,
5: 1,
6: 1,
7: 1,
8: 1,
9: 1,
10: 1,
11: 1,
12: 2,
13: 1,
14: 1,
15: 1,
16: 1,
17: 1,
18: 1,
19: 3},
'cust_age': {0: 20,
1: 34,
2: 34,
3: 34,
4: 34,
5: 26,
6: 26,
7: 24,
8: 27,
9: 35,
10: 29,
11: 29,
12: 28,
13: 29,
14: 19,
15: 32,
16: 31,
17: 21,
18: 29,
19: 29},
'cust_state': {0: 'New York',
1: 'New York',
2: 'New York',
3: 'New York',
4: 'New York',
5: 'New Jersey',
6: 'Pennsylvania',
7: 'New Jersey',
8: 'New York',
9: 'Connecticut',
10: 'Connecticut',
11: 'Connecticut',
12: 'Pennsylvania',
13: 'New York',
14: 'Pennsylvania',
15: 'New Jersey',
16: 'Connecticut',
17: 'Connecticut',
18: 'Pennsylvania',
19: 'Pennsylvania'},
'prod_price': {0: 72.99,
1: 18.95,
2: 72.99,
3: 28.45,
4: 18.95,
5: 24.95,
6: 60.99,
7: 65.99,
8: 18.95,
9: 9.95,
10: 28.45,
11: 9.95,
12: 65.99,
13: 10.97,
14: 45.99,
15: 35.99,
16: 18.95,
17: 10.97,
18: 18.95,
19: 35.99},
'prod_title': {0: 'Cat Cave',
1: 'Purrfect Puree',
2: 'Cat Cave',
3: 'Ball and String',
4: 'Yum Fish-Dish',
5: 'Fetch Blaster',
6: 'Reddy Beddy',
7: 'Reddy Beddy',
8: 'Tuna Tasties',
9: 'All Veggie Yummies',
10: 'Ball and String',
11: 'All Veggie Yummies',
12: 'Reddy Beddy',
13: 'Chewie Dental',
14: 'Snoozer Hammock',
15: 'Kitty Climber',
16: 'Purrfect Puree',
17: 'Chewie Dental',
18: 'Tuna Tasties',
19: 'Kitty Climber'},
'prod_category': {0: 'bedding',
1: 'treat',
2: 'bedding',
3: 'toy',
4: 'food',
5: 'toy',
6: 'bedding',
7: 'bedding',
8: 'treat',
9: 'treat',
10: 'toy',
11: 'treat',
12: 'bedding',
13: 'treat',
14: 'bedding',
15: 'toy',
16: 'treat',
17: 'treat',
18: 'treat',
19: 'toy'},
'prod_animal_type': {0: 'cat',
1: 'cat',
2: 'cat',
3: 'cat',
4: 'cat',
5: 'dog',
6: 'dog',
7: 'dog',
8: 'cat',
9: 'dog',
10: 'cat',
11: 'dog',
12: 'dog',
13: 'dog',
14: 'cat',
15: 'cat',
16: 'cat',
17: 'dog',
18: 'cat',
19: 'cat'},
'total_sales': {0: 72.99,
1: 18.95,
2: 72.99,
3: 56.9,
4: 18.95,
5: 24.95,
6: 60.99,
7: 65.99,
8: 18.95,
9: 9.95,
10: 28.45,
11: 9.95,
12: 131.98,
13: 10.97,
14: 45.99,
15: 35.99,
16: 18.95,
17: 10.97,
18: 18.95,
19: 107.97}}
`
Solution 1:[1]
You're looking for help on indexing and selecting data. This should work for your specific problem
df = pd.DataFrame({
'prod_title' : np.random.permutation([l for l in 'abcdefghijklmnopqrstuvwxyz']),
'total_sales' : np.random.randint(low=10000, high=400000, size=26)
})
df.sort_values(by='total_sales')['prod_title'].values[:10]
>>> array(['f', 'u', 'x', 's', 'y', 'l', 'e', 'q', 'n', 'c'], dtype=object)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Coup |

