How to convert a pandas GroupBy to a DataFrame




# Create a new DataFrameGroupBy object that groups the data by registration year
group_by_years = df.groupby(df["month"].dt.year)
# Let's count how many rows are there in each year
group_by_years_count = group_by_years.count()
# We have too many columns to display, let's reduce the number of columns to just one
group_by_years_count = group_by_years_count[["month"]]
# rename the column so it looks more proper on the barplot later
group_by_years_count.rename(columns={'month':'count'},inplace=True)
group_by_years_count.reset_index(inplace=True)
df_new = group_by_years_count.apply(list).apply(pd.Series)
df_new.columns = ['month', 'count']

Comments

Popular posts from this blog

How to create an organizational chart in your webpage using Google Organization Chart Tools

How to remove “Git” from Windows 7 context menu

How to add Diggit, Technorati, Del.icio.us, Stumbleupon, Reddit and pInterest buttons to your Blogger posts