Posts

Showing posts with the label convert

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' ]