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:
- How can I modify this code so as not to produce the warning? (without just turning the warning off!)
- Is there a better/more elegant way to achieve the same result?