In [8]: foods = pd.read_csv('Pandas/pandas/foods.csv')
In [9]: foods.head(4)
Out[9]:
First Name Gender City Frequency Item Spend
0 Wanda Female Stamford Weekly Burger 15.66
1 Eric Male Stamford Daily Chalupa 10.56
2 Charles Male New York Never Sushi 42.14
3 Anna Female Philadelphia Once Ice Cream 11.01
In [10]: foods.pivot_table(values = 'Spend',index='Gender',aggfunc='mean')
Out[10]:
Spend
Gender
Female 50.709629
Male 49.397623
In [11]: foods.pivot_table(values = 'Spend',index=['Gender','Item'],aggfunc='sum')
Out[11]:
Spend
Gender Item
Female Burger 4094.30
Burrito 4257.82
Chalupa 4152.26
Donut 4743.00
Ice Cream 4032.87
Sushi 4683.08
Male Burger 3671.43
Burrito 4012.62
Chalupa 3492.26
Donut 4015.76
Ice Cream 4854.12
Sushi 4059.85
In [12]: foods.pivot_table(values = 'Spend',index=['Gender','Item'],columns='City', aggfunc='sum')
Out[12]:
City New York Philadelphia Stamford
Gender Item
Female Burger 1239.04 1639.24 1216.02
Burrito 978.95 1458.76 1820.11
Chalupa 876.58 1673.33 1602.35
Donut 1446.78 1639.26 1656.96
Ice Cream 1521.62 1479.22 1032.03
Sushi 1480.29 1742.88 1459.91
Male Burger 1294.09 938.18 1439.16
Burrito 1399.40 1312.93 1300.29
Chalupa 1227.77 1114.23 1150.26
Donut 1345.27 1249.36 1421.13
Ice Cream 1603.63 2191.27 1059.22
Sushi 1396.15 1395.88 1267.82
In [13]: pd.pivot_table(data=foods,values = 'Spend',index=['Gender','Item'],columns='City', aggfunc='sum')
Out[13]:
City New York Philadelphia Stamford
Gender Item
Female Burger 1239.04 1639.24 1216.02
Burrito 978.95 1458.76 1820.11
Chalupa 876.58 1673.33 1602.35
Donut 1446.78 1639.26 1656.96
Ice Cream 1521.62 1479.22 1032.03
Sushi 1480.29 1742.88 1459.91
Male Burger 1294.09 938.18 1439.16
Burrito 1399.40 1312.93 1300.29
Chalupa 1227.77 1114.23 1150.26
Donut 1345.27 1249.36 1421.13
Ice Cream 1603.63 2191.27 1059.22
Sushi 1396.15 1395.88 1267.82
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment