0% found this document useful (0 votes)
761 views10 pages

Pandas Dataframe Assignment No 3 - Answerkey

The document contains 19 questions and answers related to creating pivot tables from Pandas DataFrames using titanic and sales data. The questions involve grouping and aggregating data to find things like survival rates by gender, class, age and other dimensions. Multiple indexes are used and functions like mean, count and sum are applied.

Uploaded by

Anni Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
761 views10 pages

Pandas Dataframe Assignment No 3 - Answerkey

The document contains 19 questions and answers related to creating pivot tables from Pandas DataFrames using titanic and sales data. The questions involve grouping and aggregating data to find things like survival rates by gender, class, age and other dimensions. Multiple indexes are used and functions like mean, count and sum are applied.

Uploaded by

Anni Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

PANDAS DATAFRAME

PIVOT TABLE
ASSIGNMENT NO 3 ANSWER KEY
Q1. Use Salesdata.xlsx to solve the following questions
1. Write a Pandas program to create a Pivot table with multiple indexes from a given excel
sheet (Salesdata.xlsx).

Ans:
import pandas as pd

import numpy as np
df = pd.read_excel('E:\SaleData.xlsx')
print(df)
pd.pivot_table(df,index=["Region","SalesMan"])

2. Write a Pandas program to create a Pivot table and find the total sale amount region wise,
manager wise.
Ans:

import pandas as pd
import numpy as np
df = pd.read_excel('E:\SaleData.xlsx')
pd.pivot_table(df,index = ["Region","Manager"], values =
["Sale_amt"],aggfunc=np.sum)
3. Write a Pandas program to create a Pivot table and find the total sale amount region wise,
manager wise, sales man wise.
Ans:

import pandas as pd
import numpy as np
df = pd.read_excel('E:\SaleData.xlsx')
pd.pivot_table(df,index=["Region","Manager","SalesMan"], values="Sale_amt")

4. Write a Pandas program to create a Pivot table and find the item wise unit sold.
Ans:
import pandas as pd
import numpy as np
df = pd.read_excel('E:\SaleData.xlsx')
pd.pivot_table(df,index=["Item"], values="Units")

5. Write a Pandas program to create a Pivot table and find the region wise total sale.
Ans:

import pandas as pd
import numpy as np
df = pd.read_excel('E:\SaleData.xlsx')
table = pd.pivot_table(df,index="Region",values="Sale_amt", aggfunc = np.sum)
table
6. Write a Pandas program to create a Pivot table and find the region wise, item wise unit
sold.
Ans:

import pandas as pd
import numpy as np
df = pd.read_excel('E:\SaleData.xlsx')
pd.pivot_table(df,index=["Region", "Item"], values="Units")

7. Write a Pandas program to create a Pivot table and count the manager wise sale and
mean value of sale amount.
Ans:

import numpy as np
df = pd.read_excel('E:\SaleData.xlsx')
pd.pivot_table(df,index=["Manager"],values=["Sale_amt"],aggfunc=[np.mean,len])

8. Write a Pandas program to create a Pivot table and find manager wise, salesman wise total
sale and also display the sum of all sale amount at the bottom.
Ans:

import pandas as pd
import numpy as np
df = pd.read_excel('E:\SaleData.xlsx')
table =
pd.pivot_table(df,index=["Manager","SalesMan"],values=["Units","Sale_amt"],
aggfunc=[np.sum],fill_value=0,margins=True)
print(table)

9. Write a Pandas program to create a Pivot table and find the total sale amount region wise,
manager wise, sales man wise where Manager = "Douglas".
Ans:

import pandas as pd
import numpy as np
df = pd.read_excel('E:\SaleData.xlsx')
table = pd.pivot_table(df,index=["Region","Manager","SalesMan"],
values="Sale_amt")
table.query('Manager == ["Douglas"]')

10. Write a Pandas program to create a Pivot table and find the region wise Television and
Home Theater sold.
Ans:
import pandas as pd
import numpy as np
df = pd.read_excel('E:\SaleData.xlsx')
table = pd.pivot_table(df,index=["Region", "Item"], values="Units")
table.query('Item == ["Television","Home Theater"]')

11. Write a Pandas program to create a Pivot table and find the maximum sale value of the
items.

import pandas as pd
import numpy as np
df = pd.read_excel('E:\SaleData.xlsx')
table = pd.pivot_table(df, index='Item', values='Sale_amt', aggfunc=np.max)
table

12. Write a Pandas program to create a Pivot table and find the minimum sale value of the
items.

import pandas as pd
import numpy as np
df = pd.read_excel('E:\SaleData.xlsx')
table = pd.pivot_table(df, index='Item', values='Sale_amt', aggfunc=np.min)
table
13. Write a Pandas program to create a Pivot table and find the minimum sale value of the
items.
Ans:

import pandas as pd
import numpy as np
df = pd.read_excel('E:\SaleData.xlsx')
table = pd.pivot_table(df, index='Item', values='Sale_amt', aggfunc=[np.max,
np.min])
table

Q2. Use titanic.csv to solve the following questions


1. Write a Pandas program to print a concise summary of the dataset (titanic.csv).

import pandas as pd
import numpy as np
df = pd.read_csv('titanic.csv')
result = df.info()
print(result)

2. Write a Pandas program to extract the column labels, shape and data types of the dataset
(titanic.csv).

import pandas as pd
import numpy as np
df = pd.read_csv('titanic.csv')
print("List of columns:")
print(df.columns)
print("\nShape of the Dataset:")
print(df.shape)
print("\nData types of the Dataset:")
print(df.dtypes)

3. Write a Pandas program to create a Pivot table with multiple indexes from the data set of
titanic.csv.
import pandas as pd
import numpy as np
df = pd.read_csv('titanic.csv')
result = pd.pivot_table(df, index = ["sex","age"], aggfunc=np.sum)
print(result)

4. Write a Pandas program to create a Pivot table and find survival rate by gender on various
classes.

import pandas as pd
import numpy as np
df = pd.read_csv('titanic.csv')
result = df.pivot_table('survived', index='sex', columns='class')
print(result)

5. Write a Pandas program to create a Pivot table and find survival rate by gender.

import pandas as pd
import numpy as np
df = pd.read_csv('titanic.csv')
result=df.groupby('sex')[['survived']].mean()
print(result)

6. Write a Pandas program to create a Pivot table and find survival rate by gender, age wise
of various classes.

import pandas as pd
import numpy as np
df = pd.read_csv('titanic.csv')
result = df.pivot_table('survived', index=['sex','age'], columns='class')
print(result)

7. Write a Pandas program to partition each of the passengers into four categories based on
their age. Note: Age categories (0, 10), (10, 30), (30, 60), (60, 80)

import pandas as pd
import numpy as np
df = pd.read_csv('titanic.csv')
result = pd.cut(df['age'], [0, 10, 30, 60, 80])
print(result)
8. Write a Pandas program to create a Pivot table and count survival by gender, categories
wise age of various classes.
Note: Age categories (0, 10), (10, 30), (30, 60), (60, 80)

import pandas as pd
import numpy as np
df = pd.read_csv('titanic.csv')
age = pd.cut(df['age'], [0, 10, 30, 60, 80])
result = df.pivot_table('survived', index=['sex',age], columns='pclass',
aggfunc='count')
print(result)

9. Write a Pandas program to create a Pivot table and find survival rate by gender, age of the
different categories of various classes.

import pandas as pd
import numpy as np
df = pd.read_csv('titanic.csv')
age = pd.cut(df['age'], [0, 20, 55])
result = df.pivot_table('survived', index=['sex', age], columns='class')
print(result)

10. Write a Pandas program to create a Pivot table and find survival rate by gender, age of
the different categories of various classes. Add the fare as a dimension of columns and
partition fare column into 2 categories based on the values present in fare columns.

import pandas as pd
import numpy as np
df = pd.read_csv('titanic.csv')
fare = pd.qcut(df['fare'], 2)
age = pd.cut(df['age'], [0, 10, 30, 60, 80])
result = df.pivot_table('survived', index=['sex', age], columns=[fare, 'pclass'])
print(result)

11. Write a Pandas program to create a Pivot table and calculate number of women and men
were in a particular cabin class.

import pandas as pd
import numpy as np
df = pd.read_csv('titanic.csv')
result = df.pivot_table(index=['sex'], columns=['pclass'], aggfunc='count')
print(result)

12. Write a Pandas program to create a Pivot table and find survival of both gender and class
affected.

import pandas as pd
import numpy as np
df = pd.read_csv('titanic.csv')
result = df.groupby(['sex', 'class'])['survived'].aggregate('mean').unstack()
print(result)

13. Write a Pandas program to create a Pivot table and compute survival totals of all classes
along each group.

import pandas as pd
import numpy as np
df = pd.read_csv('titanic.csv')
result = df.pivot_table('survived', index='sex', columns='class', margins=True)
print(result)

14. Write a Pandas program to create a Pivot table and calculate how many women and men
were in a particular cabin class.

import pandas as pd
import numpy as np
df = pd.read_csv('titanic.csv')
result = df.pivot_table(index=['sex'], columns=['pclass'], values='survived',
aggfunc='count')
print(result)

15. Write a Pandas program to create a Pivot table and find number of survivors and average
rate grouped by gender and class.

import pandas as pd
import numpy as np
df = pd.read_csv('titanic.csv')
result = df.pivot_table(index='sex', columns='class', aggfunc={'survived':sum,
'fare':'mean'})
print(result)
16. Write a Pandas program to create a Pivot table and find number of adult male, adult
female and children.

import pandas as pd
import numpy as np
df = pd.read_csv('titanic.csv')
result = df.pivot_table('sex', 'who', aggfunc = 'count')
print(result)

17. Write a Pandas program to create a Pivot table and check missing values of children.

import pandas as pd
import numpy as np
df = pd.read_csv('titanic.csv')
result = df.loc[df['who']=='child'].isnull().sum()
print(result)

18. Write a Pandas program to create a Pivot table and separate the gender according to
whether they traveled alone or not to get the probability of survival.

import pandas as pd
import numpy as np
df = pd.read_csv('titanic.csv')
result = df.pivot_table( 'survived' , [ 'sex' , 'alone' ] , 'class' )
print(result)

19. Write a Pandas program to create a Pivot table and find the probability of survival by
class, gender, solo boarding and port of embarkation.

import pandas as pd
import numpy as np
df = pd.read_csv('titanic.csv')
result = df.pivot_table('survived', ['sex' , 'alone' ], [ 'embark_town', 'class'
])
print(result)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy