filmov
tv
How to Create Pivot Tables with Python?

Показать описание
This video demonstrates how to create pivot tables with python and the pandas library
How do i create pivot tables with python?
Performing aggregations and pivot table computations in Python can be significantly faster and more efficient that doing it in MS Excel or Google sheets. Although it takes a bit of practice, once you lean it, the investment definitely pays off.
In this video we will review how to do analysis and aggregation using pivot tables using three different methods: Pivot, CrossTab and Groupby Methods.
##################################
CODE
##################################
# Pivot, CrossTab and Groupby
import pandas as pd
import seaborn as sns
titanic
df = titanic[['sex', 'age', 'fare', 'alive', 'adult_male', 'class']]
# by gender and survival
aggfunc='mean' )
aggfunc='mean',
normalize=True )
df[['sex','alive', 'age']].groupby(['sex','alive']).mean('age').unstack()
# gender and fare
columns='alive',
values='fare')
aggfunc='mean',
normalize=False )
df[['sex','alive', 'fare']] .groupby(['sex','alive']) .mean('fare').unstack()
# gender - adult male count
columns=['alive','class'],
values='fare',
aggfunc='count')
aggfunc='count')
df[['sex','adult_male', 'alive','class', 'fare']] .groupby(['sex','adult_male', 'alive','class']) .count() .unstack([2,3])
How do i create pivot tables with python?
Performing aggregations and pivot table computations in Python can be significantly faster and more efficient that doing it in MS Excel or Google sheets. Although it takes a bit of practice, once you lean it, the investment definitely pays off.
In this video we will review how to do analysis and aggregation using pivot tables using three different methods: Pivot, CrossTab and Groupby Methods.
##################################
CODE
##################################
# Pivot, CrossTab and Groupby
import pandas as pd
import seaborn as sns
titanic
df = titanic[['sex', 'age', 'fare', 'alive', 'adult_male', 'class']]
# by gender and survival
aggfunc='mean' )
aggfunc='mean',
normalize=True )
df[['sex','alive', 'age']].groupby(['sex','alive']).mean('age').unstack()
# gender and fare
columns='alive',
values='fare')
aggfunc='mean',
normalize=False )
df[['sex','alive', 'fare']] .groupby(['sex','alive']) .mean('fare').unstack()
# gender - adult male count
columns=['alive','class'],
values='fare',
aggfunc='count')
aggfunc='count')
df[['sex','adult_male', 'alive','class', 'fare']] .groupby(['sex','adult_male', 'alive','class']) .count() .unstack([2,3])
Комментарии