Python for Data Analyst

Pandas Example – Write a Pandas program to print a DataFrame without index

(Python Example for Beginners)   Write a Pandas program to print a DataFrame without index.   Sample Solution: Python Code : import pandas as pd df = pd.DataFrame({ ‘school_code’: [‘s001′,’s002′,’s003′,’s001′,’s002′,’s004’], ‘class’: [‘V’, ‘V’, ‘VI’, ‘VI’, ‘V’, ‘VI’], ‘name’: [‘Alberto Franco’,’Gino Mcneill’,’Ryan Parkes’, ‘Eesha Hinton’, ‘Gino Mcneill’, ‘David Parkes’], ‘date_of_birth’: [’15/05/2002′,’17/05/2002′,’16/02/1999′,’25/09/1998′,’11/05/2002′,’15/09/1997′], ‘weight’: [35, 32, 33, 30, …

Pandas Example – Write a Pandas program to drop a index level from a multi-level column index of a dataframe

(Python Example for Beginners)   Write a Pandas program to drop a index level from a multi-level column index of a dataframe. Note: Levels are 0-indexed beginning from the top.   Sample Solution: Python Code : import pandas as pd cols = pd.MultiIndex.from_tuples([(“a”, “x”), (“a”, “y”), (“a”, “z”)]) print(“nConstruct a Dataframe using the said MultiIndex …

Pandas Example – Write a Pandas program to rename names of columns and specific labels of the Main Index of the MultiIndex dataframe

(Python Example for Beginners)   Write a Pandas program to rename names of columns and specific labels of the Main Index of the MultiIndex dataframe. Sample Solution: Python Code : import pandas as pd import numpy as np sales_arrays = [[‘sale1’, ‘sale1’, ‘sale2’, ‘sale2’, ‘sale3’, ‘sale3’, ‘sale4’, ‘sale4’], [‘city1’, ‘city2’, ‘city1’, ‘city2’, ‘city1’, ‘city2’, ‘city1’, …

Pandas Example – Write a Pandas program to extract a single row, rows and a specific value from a MultiIndex levels DataFrame

(Python Example for Beginners)   Write a Pandas program to extract a single row, rows and a specific value from a MultiIndex levels DataFrame.   Sample Solution: Python Code : import pandas as pd import numpy as np sales_arrays = [[‘sale1’, ‘sale1’, ‘sale2’, ‘sale2’, ‘sale3’, ‘sale3’, ‘sale4’, ‘sale4’], [‘city1’, ‘city2’, ‘city1’, ‘city2’, ‘city1’, ‘city2’, ‘city1’, …

Pandas Example – Write a Pandas program to construct a series using the MultiIndex levels as the column and index

(Python Example for Beginners)   Write a Pandas program to construct a series using the MultiIndex levels as the column and index.   Sample Solution: Python Code : import pandas as pd import numpy as np sales_arrays = [[‘sale1’, ‘sale1’, ‘sale2’, ‘sale2’, ‘sale3’, ‘sale3’, ‘sale4’, ‘sale4’], [‘city1’, ‘city2’, ‘city1’, ‘city2’, ‘city1’, ‘city2’, ‘city1’, ‘city2’]] sales_tuples …

Pandas Example – Write a Pandas program to convert index of a given dataframe into a column

(Python Example for Beginners)   Write a Pandas program to convert index of a given dataframe into a column. Test Data: 0 s001 V Alberto Franco 15/05/2002 35 street1 t1 1 s002 V Gino Mcneill 17/05/2002 32 street2 t2 2 s003 VI Ryan Parkes 16/02/1999 33 street3 t3 3 s001 VI Eesha Hinton 25/09/1998 30 …

Pandas Example – Write a Pandas program to create a dataframe indexing by date and time

(Python Example for Beginners)   Write a Pandas program to create a dataframe indexing by date and time. Test Data: 0 s001 V Alberto Franco 15/05/2002 35 street1 t1 1 s002 V Gino Mcneill 17/05/2002 32 street2 t2 2 s003 VI Ryan Parkes 16/02/1999 33 street3 t3 3 s001 VI Eesha Hinton 25/09/1998 30 street1 …

Pandas Example – Write a Pandas program to display the default index and set a column as an Index in a given dataframe

(Python Example for Beginners)   Write a Pandas program to display the default index and set a column as an Index in a given dataframe. Test Data: 0 s001 V Alberto Franco 15/05/2002 35 street1 t1 1 s002 V Gino Mcneill 17/05/2002 32 street2 t2 2 s003 VI Ryan Parkes 16/02/1999 33 street3 t3 3 …

Pandas Example – Write a Pandas program to get the numeric representation of an array by identifying distinct values of a given column of a DataFrame

(Python Example for Beginners)   Write a Pandas program to get the numeric representation of an array by identifying distinct values of a given column of a DataFrame.   Sample Solution : Python Code : import pandas as pd df = pd.DataFrame({ ‘Name’: [‘Alberto Franco’,’Gino Mcneill’,’Ryan Parkes’, ‘Eesha Hinton’, ‘Gino Mcneill’], ‘Date_Of_Birth ‘: [’17/05/2002′,’16/02/1999′,’25/09/1998′,’11/05/2002′,’15/09/1997’], ‘Age’: [18.5, …