I am new to pandas and hence please treat this question with patience I have a Df with year, state and population data collected over many years and across many states
I want to find the max pop during any year and the corresponding state
example:
1995 Alabama xx; 1196 New York yy; 1997 Utah zz
I did a groupby and got the population for all the states in a year; How do i iterate over all the years
state_yearwise = df.groupby(["Year", "State"])["Pop"].max()
state_yearwise.head(10)
1990 Alabama 22.5
Arizona 29.4
Arkansas 16.2
California 34.1
2016 South Dakota 14.1
Tennessee 10.2
Texas 17.4
Utah 16.1
Now I did
df.loc[df.pop == df.pop.max(), ["year", "State", "pop"]]
1992 Colorado 54.1
give me only 1 year and the max over all years and states What I want is per year which state had the max population
Suggestions?
Is this what you want:
I see for
df
:And for the final result:
Proof this works: