<jupyter_start><jupyter_text>Introduction to Python   Pandas Descriptive Statistics and Visualization<jupyter_code>import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns<jupyter_output><empty_output><jupyter_text>Reading Dataframe from Excel<jupyter_code>dfvote = pd.read_excel(os.path.join('../Data','votesurvey.xls'))<jupyter_output><empty_output><jupyter_text>First steps examining data<jupyter_code>dfvote.head(6)
dfvote.tail(4)
dfvote.info()
dfvote.describe()<jupyter_output><empty_output><jupyter_text>Plotting: Histogram<jupyter_code># Using Matplotlib calls

fig = plt.figure(figsize=(15,8))
#Create one or more subplots using add_subplot, because you can't create blank figure
ax = fig.add_subplot(1,1,1)

#Variable
ax.hist(dfvote['Age'],bins = 8) # Here you can play with number of bins Labels and Tit

plt.title('Age distribution')
plt.xlabel('Age')
plt.ylabel('#Citizens')
plt.grid()
plt.show()
# Plotting within Pandas

dfvote.Age.hist(figsize=(15,8), bins=8);
# Using Matplotlib calls

fig = plt.figure(figsize=(15,8))
ax = fig.add_subplot(1,1,1)

ax.boxplot(dfvote['Age'])

plt.show()<jupyter_output><empty_output><jupyter_text>Box Plot<jupyter_code># Plotting within Pandas

dfvote.boxplot(by='Age', figsize=(15,8));<jupyter_output>/opt/conda/lib/python3.7/site-packages/numpy/core/_asarray.py:83: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
  return array(a, dtype, copy=False, order=order)
/opt/conda/lib/python3.7/site-packages/numpy/core/_asarray.py:83: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
  return array(a, dtype, copy=False, order=order)<jupyter_text>Violin Plot (using Seaborn)<jupyter_code>fig = plt.figure(figsize=(15,8))

sns.violinplot(dfvote['Age'], dfvote['Gender'])

sns.despine()<jupyter_output><empty_output><jupyter_text>Bar Chart<jupyter_code>dfvote.groupby('Gender').head()
var = dfvote.groupby('Candidate').Age.mean() #grouped sum of <randoms> at Gender level
fig = plt.figure(figsize=(15,8))
ax1 = fig.add_subplot(1,1,1)


var.plot(kind='bar');<jupyter_output><empty_output><jupyter_text>Line Chart<jupyter_code>var = dfvote.groupby('Candidate').Age.mean()

fig = plt.figure(figsize=(15,8))
ax1 = fig.add_subplot(1,1,1)
#ax1.set_xlabel('Candidate')
ax1.set_ylabel('Mean of Ages')
ax1.set_title("Candidate wise mean of ages")

var.plot(ax=ax1, kind='line')<jupyter_output><empty_output><jupyter_text>Stacked Column Chart<jupyter_code>var = dfvote.groupby(['Age','Gender']).Random.sum()
var.unstack().plot(kind='bar',stacked=True,  color=['red','blue'], grid=False, figsize=(15,8))<jupyter_output><empty_output><jupyter_text>Scatter Plot<jupyter_code>fig = plt.figure(figsize=(15,8))
ax = fig.add_subplot(1,1,1)
ax.scatter(dfvote['Age'],dfvote['Random'],s=dfvote['Expected salary']/500) #You can also add more variables here to represent color and size.
plt.show()
dfvote.plot.scatter(x='Age',y='Expected salary', c='Random', cmap='jet', figsize=(15,8));<jupyter_output><empty_output><jupyter_text>Bubble Plot<jupyter_code>fig = plt.figure(figsize=(15,8))
ax = fig.add_subplot(1,1,1)
# Added third variable income as size of the bubble
ax.scatter(dfvote['Age'],dfvote['Expected salary'], s=dfvote['Random']**3)
plt.show()<jupyter_output><empty_output><jupyter_text>Pie chart<jupyter_code>var=dfvote.groupby(['Gender']).sum().stack()
temp=var.unstack()
type(temp)
x_list = temp['Random']
label_list = temp.index
#The pie chart is oval by default. To make it a circle use plt.axis("equal")
fig = plt.figure(figsize=(15,15))
plt.axis("equal")
#To show the percentage of each pie slice, pass an output format to the autopctparameter 
plt.pie(x_list,labels=label_list,autopct="%1.1f%%") 
plt.title("Gender Distribution")
plt.show()<jupyter_output><empty_output><jupyter_text>Heat Map<jupyter_code>#Generate a random number, you can refer your data values also
data = np.random.rand(8,2)
rows = list('12345678') #rows categories
columns = list('MF') #column categories

fig,ax=plt.subplots(figsize=(15,15))
#Advance color controls
ax.pcolor(data,cmap=plt.cm.Reds,edgecolors='k')
# Here we position the tick labels for x and y axis
ax.set_xticks(np.arange(0,2)+0.5)
ax.set_yticks(np.arange(0,8)+0.5)
ax.xaxis.tick_top()
ax.yaxis.tick_left()
#Values against each labels
ax.set_xticklabels(columns,minor=False,fontsize=20)
ax.set_yticklabels(rows,minor=False,fontsize=20)
plt.show()
print(data)
dfvote.sort_values(by=['Age','Expected salary'], ascending=[True, False])[0:10]<jupyter_output><empty_output><jupyter_text>A Case Study<jupyter_code>df = pd.read_csv('../Data/MELBOURNE_HOUSE_PRICES_LESS.csv')
df.head()
df.tail()
df.info()
df['Date'] = pd.to_datetime(df['Date'])
df['Type'] = df['Type'].astype('category')
df['Method'] = df['Method'].astype('category')
df.info()
df['Type'].unique()
df['Type'].value_counts()
average = df['Price'].mean()
print('{:.2f}'.format(average))
med = df['Price'].median()
print('{:.2f}'.format(med))
standard_deviation = df['Price'].std()
print('{:.2f}'.format(standard_deviation))
df.describe()
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

sns.set(style="whitegrid")

plt.figure(figsize=(8,12))
ax = sns.boxplot(x='Price', data=df, orient="v")
plt.figure(figsize=(12,10))
ax = sns.boxplot(x='Type', y='Price', data=df, orient="v")
filter_data = df.dropna(subset=['Price'])
plt.figure(figsize=(14,8))

sns.distplot(filter_data['Price'], kde=False)
type_counts = df['Type'].value_counts()

df2 = pd.DataFrame({'house_type': type_counts}, index = ['t', 'h', 'u'])

df2.plot.pie(y='house_type', figsize=(10,10), autopct='%1.1f%%')
sns.set(style='darkgrid')
plt.figure(figsize=(20,10))

ax = sns.countplot(x='Regionname', data=df)
df.head()
sns.lmplot('Price', 'Postcode', data=df, fit_reg=False, scatter_kws={"marker": "D", "s": 20}) 
plt.title('Scatter Plot of Data without Regression Line')
plt.xlabel('Price')
plt.ylabel('Post Code')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1691026245481691, 'fraction_numerical': 0.021216407355021217, 'mean_word_length': 8.039772727272727, 'pattern_counts': {'":': 2, '<': 57, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '38620', 'license': 'Apache-2.0', 'n_tokens_mistral': 2503, 'n_tokens_neox': 2392, 'n_words': 451, 'path': 'Notebooks/16_Pandas_Descriptive_statistic.ipynb', 'repo_name': 'stkiesling/Python_Course', 'size': '6363'}