Tag Archives: python for business analysts

Pandas Example – Write a Pandas program to replace more than one value with other values in a given DataFrame

(Python Example for Beginners)   Write a Pandas program to replace more than one value with other values in a given DataFrame.   Sample Solution: Python Code : import pandas as pd df = pd.DataFrame({ ‘company_code’: [‘A’,’B’, ‘C’, ‘D’, ‘A’], ‘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(“nReplace A with …

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 filter words from a given series that contain atleast two vowels

(Python Example for Beginners)   Write a Pandas program to swap the cases of a specified character column 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(“nSwapp cases in …

Pandas Example – Write a Pandas program to filter words from a given series that contain atleast two vowels

(Python Example for Beginners)   Write a Pandas program to check if a specified column starts with a specified string in a 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(“nIf a …

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 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 …

Write a Pandas program to find the index of a substring of DataFrame with beginning and end position

(Python Example for Beginners)   Write a Pandas program to find the index of a substring of DataFrame with beginning and end position. Sample Solution: Python Code : import pandas as pd df = pd.DataFrame({ ‘name_code’: [‘c0001′,’1000c’,’b00c2′, ‘b2c02’, ‘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(“\nIndex of a …

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 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 …