简体   繁体   中英

Grouping rows for a dataframe in Pandas

I currently have a dataframe called UniqueItems that looks like this (I shortened it because it is a long list):

# of Transactions
Item    
Adjustment  1
Afternoon with the baker    44
Alfajores   369
Argentina Night 7
Art Tray    38

I sorted the values out to give me a new dataframe called UniqueItem_inorder which shows the top 5 transactions through this code:

UniqueItem_inorder = UniqueItem.sort_values(by=['# of Transactions'], ascending=False).head(5)

If I want to group the rest of the items into another row called "other" from UniqueItem that are not in UniqueItem_inorder, how would I go about it?

Some variation of the following could work:

import string
import numpy as np
import pandas as pd

np.random.seed(2)    
df = pd.DataFrame({'Item': list(string.ascii_lowercase),
                   'Number': np.random.randint(1, 300, size=26)})
df.sort_values(by='Number', ascending=False, inplace=True)
leftover = df['Number'].iloc[5:].sum()
df = df.append({'Item': 'other', 'Number': leftover}, ignore_index=True)
print(df.head())
print(df.tail())

which results in:

  Item  Number
0    p     294
1    l     288
2    f     264
3    t     261
4    y     256
     Item  Number
22      v      39
23      w      34
24      d      23
25      b      16
26  other    2155

If you want to remove items 5 to 25 from the dataframe, just ignore those when appending the 'other' row:

<as before>
df = df[:5].append({'Item': 'other', 'Number': leftover}, ignore_index=True)
print(df)

Result:

    Item  Number
0      p     294
1      l     288
2      f     264
3      t     261
4      y     256
5  other    2155

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM