Day: April 8, 2021

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

Hits: 10 (Python Example for Beginners)   Write a Pandas program to construct a DataFrame 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’, …

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

Hits: 4 (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’, …

Pandas Example – Write a Pandas program to convert 1st and 3rd levels in the index into columns from a multiple level of index frame of a given dataframe

Hits: 2 (Python Example for Beginners)   Write a Pandas program to convert 1st and 3rd levels in the index into columns from a multiple level of index frame of 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 …

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

Hits: 2 (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 …

Pandas Example – Write a Pandas program to create a dataframe and set a title or name of the index column

Hits: 3 (Python Example for Beginners)   Write a Pandas program to create a dataframe and set a title or name of the index 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 …

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

Hits: 5 (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 …

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

Hits: 6 (Python Example for Beginners)   Write a Pandas program to display the default index and set a column as an Index in a given dataframe and then reset the index. 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 …

Pandas Example – Write a Pandas program to create a multi Index frame using two columns and using an Index and a column

Hits: 2 (Python Example for Beginners)   Write a Pandas program to create a multi Index frame using two columns and using an Index and 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 …

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

Hits: 3 (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 …

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

Hits: 6 (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’], …

Pandas Example – Write a Pandas program to fill missing values in time series data

Hits: 2 (Python Example for Beginners)   Write a Pandas program to fill missing values in time series data. From Wikipedia , in the mathematical field of numerical analysis, interpolation is a type of estimation, a method of constructing new data points within the range of a discrete set of known data points.   Sample …

Pandas Example – Write a Pandas program to combine many given series to create a DataFrame

Hits: 1 (Python Example for Beginners)   Write a Pandas program to combine many given series to create a DataFrame.   Sample Solution : Python Code : import pandas as pd sr1 = pd.Series([‘php’, ‘python’, ‘java’, ‘c#’, ‘c++’]) sr2 = pd.Series([1, 2, 3, 4, 5]) print(“Original Series:”) print(sr1) print(sr2) print(“Combine above series to a dataframe:”) ser_df …

Pandas Example – Write a Pandas program to convert continuous values of a column in a given DataFrame to categorical

Hits: 2 (Python Example for Beginners)   Write a Pandas program to convert continuous values of a column in a given DataFrame to categorical. Input: { ‘Name’: [‘Alberto Franco’,’Gino Mcneill’,’Ryan Parkes’, ‘Eesha Hinton’, ‘Syed Wharton’], ‘Age’: [18, 22, 40, 50, 80, 5] } Output: Age group: 0 kids 1 adult 2 elderly 3 adult 4 …

Pandas Example – Write a Pandas program to rename all columns with the same pattern of a given DataFrame

Hits: 1 (Python Example for Beginners)   Write a Pandas program to rename all columns with the same pattern of a given DataFrame.   Sample Solution : Python Code : import pandas as pd df = pd.DataFrame({ ‘Name’: [‘Alberto Franco’,’Gino Mcneill’,’Ryan Parkes’, ‘Eesha Hinton’, ‘Syed Wharton’], ‘Date_Of_Birth ‘: [’17/05/2002′,’16/02/1999′,’25/09/1998′,’11/05/2002′,’15/09/1997’], ‘Age’: [18.5, 21.2, 22.5, 22, 23] }) …

Pandas Example – Write a Pandas program to select columns by data type of a given DataFrame

Hits: 0 (Python Example for Beginners)   Write a Pandas program to select columns by data type of a given DataFrame.   Sample Solution : Python Code : import pandas as pd df = pd.DataFrame({ ‘name’: [‘Alberto Franco’,’Gino Mcneill’,’Ryan Parkes’, ‘Eesha Hinton’, ‘Syed Wharton’], ‘date_of_birth’: [’17/05/2002′,’16/02/1999′,’25/09/1998′,’11/05/2002′,’15/09/1997′], ‘age’: [18.5, 21.2, 22.5, 22, 23] }) print(“Original DataFrame”) print(df) …

Pandas Example – Write a Pandas program to add a prefix or suffix to all columns of a given DataFrame

Hits: 0 (Python Example for Beginners)   Write a Pandas program to add a prefix or suffix to all columns of a given DataFrame.   Sample Solution : Python Code : import pandas as pd df = pd.DataFrame({‘W’:[68,75,86,80,66],’X’:[78,85,96,80,86], ‘Y’:[84,94,89,83,86],’Z’:[86,97,96,72,83]}); print(“Original DataFrame”) print(df) print(“nAdd prefix:”) print(df.add_prefix(“A_”)) print(“nAdd suffix:”) print(df.add_suffix(“_1”)) Sample Output: Original DataFrame W X Y Z …