'How to add The numbers in the rows and create a list out of each year's sum? (python)

I have a dataset about the number of forest fires in different states in Brazil. I want to add the number of forest fires each year in all states and create a list when I give the function a range of years. For example, if I give the function from 1998 to 2002 it gives me a list of 5 numbers which each are the sum of the number of forest fires in all states in that year. I managed to figure out how to get the sum for all states for specific years, it's just that I don't know how to put them in a list since it keeps giving me the title of the table also when I do the sum. Here's the code:

    states_per_year = pd.pivot_table(df_ff.drop(['date', 'month'], axis = 1), values = 'number', columns = 'state', index = 'year', aggfunc = np.sum)
list1 = []
list1.append(states_per_year.loc[1998:2002].sum(axis=1))
print(list1)


#### OUTPUT:
[year
1998    20013.971
1999    26882.821
2000    27351.251
2001    29071.612
2002    37390.600
dtype: float64]

I want only the result of the sums to be in a list. In other words, I want the output to be the following:

[20013.971, 26882.821, 27351.251, 29071.612,37390.600]

Thank you in advance!!



Solution 1:[1]

You're adding pd.Series as one item into the list1. Try instead:

list1 = states_per_year.loc[1998:2002].sum(axis=1).tolist()

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 Andrej Kesely