Python for Data Analyst

Pandas Example – Write a Pandas program to convert a specified character column in title case in a given DataFrame

(Python Example for Beginners)   Write a Pandas program to convert a specified character column in title case in a given DataFrame.   Sample Solution: Python Code : import pandas as pd df = pd.DataFrame({ ‘company_code’: [‘Abcd’,’EFGF’, ‘zefsalf’, ‘sdfslew’, ‘zekfsdf’], ‘date_of_sale’: [’12/05/2002′,’16/02/1999′,’25/09/1998′,’12/02/2022′,’15/09/1997′], ‘sale_amount’: [12348.5, 233331.2, 22.5, 2566552.0, 23.0] }) print(“Original DataFrame:”) print(df) print(“nTitle cases:”) df[‘company_code_title_cases’] …

Pandas Example – Write a Pandas program to get the length of the string present of a given column in a DataFrame

(Python Example for Beginners)   Write a Pandas program to get the length of the string present of a given column in a DataFrame.   Sample Solution: Python Code : import pandas as pd df = pd.DataFrame({ ‘company_code’: [‘Abcd’,’EFGF’, ‘skfsalf’, ‘sdfslew’, ‘safsdf’], ‘date_of_sale ‘: [’12/05/2002′,’16/02/1999′,’25/09/1998′,’12/02/2022′,’15/09/1997’], ‘sale_amount’: [12348.5, 233331.2, 22.5, 2566552.0, 23.0]}) print(“Original DataFrame:”) print(df) print(“nLength …

Pandas Example – Write a Pandas program to check whether only space is present in a given column of a DataFrame

(Python Example for Beginners)   Write a Pandas program to check whether only space is present in a given column of a DataFrame.   Sample Solution: Python Code : import pandas as pd df = pd.DataFrame({ ‘company_code’: [‘Abcd’,’EFGF ‘, ‘ ‘, ‘abcd’, ‘ ‘], ‘date_of_sale ‘: [’12/05/2002′,’16/02/1999′,’25/09/1998′,’12/02/2022′,’15/09/1997’], ‘sale_amount’: [12348.5, 233331.2, 22.5, 2566552.0, 23.0]}) print(“Original DataFrame:”) …

Pandas Example – Write a Pandas program to check whether only lower case or upper case is present in a given column of a DataFrame

(Python Example for Beginners)   Write a Pandas program to check whether only lower case or upper case is present in a given column of a DataFrame.   Sample Solution: Python Code : import pandas as pd df = pd.DataFrame({ ‘company_code’: [‘ABCD’,’EFGF’, ‘hhhh’, ‘abcd’, ‘EAWQaaa’], ‘date_of_sale ‘: [’12/05/2002′,’16/02/1999′,’25/09/1998′,’12/02/2022′,’15/09/1997’], ‘sale_amount’: [12348.5, 233331.2, 22.5, 2566552.0, 23.0]}) print(“Original …

Pandas Example – Write a Pandas program to check whether only numeric values present in a given column of a DataFrame

(Python Example for Beginners)   Write a Pandas program to check whether only numeric values present in a given column of a DataFrame.   Sample Solution: Python Code : import pandas as pd df = pd.DataFrame({ ‘company_code’: [‘Company’,’Company a001′, ‘2055’, ‘abcd’, ‘123345’], ‘date_of_sale ‘: [’12/05/2002′,’16/02/1999′,’25/09/1998′,’12/02/2022′,’15/09/1997’], ‘sale_amount’: [12348.5, 233331.2, 22.5, 2566552.0, 23.0]}) print(“Original DataFrame:”) print(df) print(“nNumeric …

Pandas Example – Write a Pandas program to check whether alphabetic values present in a given column of a DataFrame

(Python Example for Beginners)   Write a Pandas program to check whether alphabetic values present in a given column of a DataFrame. Note: isalpha() returns True if all characters in the string are alphabetic and there is at least one character, False otherwise.   Sample Solution: Python Code : import pandas as pd df = …

Write a Pandas program to find the index of a given substring of a DataFrame column

(Python Example for Beginners)   Write a Pandas program to find the index of a given substring of a DataFrame column.   Sample Solution: Python Code : import pandas as pd df = pd.DataFrame({ ‘name_code’: [‘c001′,’c002′,’c022’, ‘c2002’, ‘c2222’], ‘date_of_birth ‘: [’12/05/2002′,’16/02/1999′,’25/09/1998′,’12/02/2022′,’15/09/1997’], ‘age’: [18.5, 21.2, 22.5, 22, 23] }) print(“Original DataFrame:”) print(df) print(“\nCount occurrence of 22 …

Pandas Example – Write a Pandas program to capitalize all the string values of specified columns of a given DataFrame

(Python Example for Beginners)   Write a Pandas program to capitalize all the string values of specified columns of a given DataFrame.   Sample Solution: Python Code : import pandas as pd df = pd.DataFrame({ ‘name’: [‘alberto’,’gino’,’ryan’, ‘Eesha’, ‘syed’], ‘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) print(“nAfter capitalizing name …

Pandas Example – Write a Pandas program to convert all the string values to upper, lower cases in a given pandas series. Also find the length of the string values

(Python Example for Beginners)   Write a Pandas program to convert all the string values to upper, lower cases in a given pandas series. Also find the length of the string values.   Sample Solution: Python Code : import pandas as pd import numpy as np s = pd.Series([‘X’, ‘Y’, ‘Z’, ‘Aaba’, ‘Baca’, np.nan, ‘CABA’, …

Pandas Example – Write a Pandas program to select rows by filtering on one or more column(s) in a multi-index dataframe

(Python Example for Beginners)   Write a Pandas program to select rows by filtering on one or more column(s) in a multi-index dataframe.   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 …