0

I have the following Pandas dataframe:

seating_totals = pd.DataFrame({'seating_type':['Sit Down', 'Spinning', 'Inverted', 'Suspended', 'Alpine', 'na', 'Water Coaster', 'Flying', 'Floorless', 'Motorbike', 'Stand Up', '4th Dimension', 'Wing', 'Bobsleigh', 'Pipeline'], 'total':[2217, 150, 122, 55, 43, 40, 29, 27, 27, 21, 20, 18, 17, 11, 5]})

     seating_type  total
0        Sit Down   2217
1        Spinning    150
2        Inverted    122
3       Suspended     55
4          Alpine     43
5              na     40
6   Water Coaster     29
7          Flying     27
8       Floorless     27
9       Motorbike     21
10       Stand Up     20
11  4th Dimension     18
12           Wing     17
13      Bobsleigh     11
14       Pipeline      5

For the purposes of plotting I want to combine the smaller categories into 'Other' to produce a dataframe as follows:

  seating_type  total
0     Sit Down   2217
1        Other    313
2     Spinning    150
3     Inverted    122

The following code works:

mask = seating_totals.total < 100
seating_totals.seating_type[mask] = 'Other'
seating_totals = seating_totals.groupby('seating_type').sum().sort_values('total', ascending=False).reset_index()

However the second line produces the warning SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. So my questions are:

  1. How can I modify this code so as not to produce the warning? (without just turning the warning off!)
  2. Is there a better/more elegant way to achieve the same result?
  • As for the warning: look [here](https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas). The warning should disappear if you assign the new values like so: `seating_totals['seating_type'][mask] = 'Other'` – Lukas Thaler Feb 18 '21 at 13:15
  • nice name though.. :) – sophocles Feb 18 '21 at 13:18
  • Thanks for the link @LukasThaler. I found `seating_totals.loc[mask, 'seating_type] ` avoided the warning – sophocles99 Feb 19 '21 at 14:28
  • Haha! Thanks @sophocles, you too! That goes some way to cheering me up after the shame of my very first question turning out to be a duplicate... – sophocles99 Feb 19 '21 at 14:30

0 Answers0