Posts

Showing posts with the label groupby

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

Load data from CSV and plot using Seaborn barplot method

Image
For my PDS class! %matplotlib inline import mysql.connector from datetime import date, datetime import sys import pandas as pd import seaborn as sns user,pw, host,db = 'root','it8701','127.0.0.1','anotherdatabase2' cnx = mysql.connector.connect(user=user, password=pw, host=host, database=db) cursor = cnx.cursor() select_stmt = ("SELECT * FROM cea_salespersons") try:   cursor.execute(select_stmt)   df = pd.DataFrame(cursor.fetchall(), columns = ['cea_salesperson_id','salesperson_name', 'registration_no',                                                   'registration_start_date',  'registration_end_date',                                                    'estate_agent_name','estate_agent_license_no...