Created
May 18, 2019 17:39
-
-
Save netsatsawat/fdfa1d8cf1261a7c00d00a1341ba5e0d to your computer and use it in GitHub Desktop.
Function to plot categorical data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def plot_categorical(df: pd.DataFrame , col:str): | |
""" | |
Function to plot the categorical data on piechart using Plotly | |
@Args: | |
df: pandas data frame | |
col: A string column name within pandas data frame to plot | |
Return: | |
No object return, only visualization | |
""" | |
value_ = df[col].value_counts().values | |
idx_ = df[col].value_counts().index | |
trace = [{'values': value_, | |
'labels': idx_, | |
'name': col, | |
'hoverinfo': 'label+value+name', | |
'hole': 0.4, | |
'type': 'pie' | |
}] | |
layout = {'title': '<b>%s</b> categorical distribution' % col, | |
'paper_bgcolor': '#e8e8e8', | |
'plot_bgcolor': '#e8e8e8', | |
'autosize': False, | |
'width': 800, | |
'height': 400, | |
'annotations': [{'text' : '<b>%s</b>' % col, | |
'font': {'size': 11, | |
'color': 'black'}, | |
'x': 0.5, | |
'y': 0.5, | |
'showarrow': False | |
}] | |
} | |
py.iplot({'data': trace, 'layout': layout}) | |
le_10_val_col = [] | |
mt_10_val_col = [] | |
obj_val_col = [] | |
for col in data_df.columns: | |
if (len(data_df[col].unique()) <= 10) & (data_df[col].dtypes == 'int64'): | |
le_10_val_col.append(col) | |
elif (len(data_df[col].unique()) > 10) & (data_df[col].dtypes == 'int64'): | |
mt_10_val_col.append(col) | |
elif data_df[col].dtypes == 'O': | |
obj_val_col.append(col) | |
for i in obj_val_col: | |
plot_categorical(data_df, i) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment