Skip to content
Advertisement

Group by Issue with Years Pandas

I’m following the answer for this StackOverflow post to group a column of years by decades to make it easier for me to visualize later, but I’m not getting the same results. It seems like when DSM did it, it yielded integers for years, while mine is yielding floats for years.

I’ve implemented:

p_b['yr_built'] = pd.to_datetime(df['yr_built'], format='%Y', errors='coerce').dt.to_period('Y')
p_b= p_b.set_index('yr_built')
p_b1= p_b.groupby((p_b.index.year/10)*10).mean()

My Results: Picture of Results

Advertisement

Answer

You should add another /, I.e. p_b.index.year//.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement