I have a GroupBy object with row indexes that are integers.
light worst_injury count
1 5 10217
2 5 4067
3 5 2142
4 5 1690
5 5 25848
6 5 734
9 5 18
I would like to re-name the rows (not the columns!) so that the 'light' column contains specific strings:
light worst_injury count
Day 5 10217
Dawn 5 4067
Dusk 5 2142
Dark- lit 5 1690
Dark- unlit 5 25848
Other 5 734
Unknown 5 18
I have a list of the strings that correspond to each number ['Day', 'Dawn', etc.] but I don't know how to set them as the index before or during the GroupBy function call. I've also tried making a pivot table but it doesn't seem possible to do it that way either for the same reasons.
I suppose I could write a script to change the original data to these strings, rather than the numbers. That seems like a less efficient way to do it, but I'm open to that option if there's no way to change the groupby object after or before the fact.
Here's the existing code; it groups the dataframe by light and each injury level, and then takes a count:
df = pd.read_csv(filename, sep='|', usecols=['crash_deer_involv_assoc', 'worst_injury_in_accident', 'light', 'accident_month'])
for i in range(1,6):
inj = df[(df['worst_injury_in_accident'] == i)]
grouped = inj.groupby(['light','worst_injury_in_accident'])
grouped.agg('count')