Python for Data Analyst

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

(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 = pd.DataFrame(sr1, …

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

(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 elderly 5 …

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

(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] }) print(“Original DataFrame”) …

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

(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) print(“nSelect numerical …

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

(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 0 68 …

Pandas Example – Write a Pandas program to remove first n rows of a given DataFrame

(Python Example for Beginners)   Write a Pandas program to remove first n rows of a given DataFrame.   Sample Solution : Python Code : import pandas as pd d = {‘col1’: [1, 2, 3, 4, 7, 11], ‘col2’: [4, 5, 6, 9, 5, 0], ‘col3’: [7, 5, 8, 12, 1,11]} df = pd.DataFrame(data=d) print(“Original DataFrame”) …

Pandas Example – Write a Pandas program to get last n records of a DataFrame

(Python Example for Beginners)   Write a Pandas program to get last n records of a DataFrame.   Sample Solution : Python Code : import pandas as pd d = {‘col1’: [1, 2, 3, 4, 7, 11], ‘col2’: [4, 5, 6, 9, 5, 0], ‘col3’: [7, 5, 8, 12, 1,11]} df = pd.DataFrame(data=d) print(“Original DataFrame”) print(df) …

Pandas Example – Write a Pandas program to select all columns, except one given column in a DataFrame

(Python Example for Beginners)   Write a Pandas program to select all columns, except one given column in a DataFrame.   Sample Solution : Python Code : import pandas as pd d = {‘col1’: [1, 2, 3, 4, 7], ‘col2’: [4, 5, 6, 9, 5], ‘col3’: [7, 8, 12, 1, 11]} df = pd.DataFrame(data=d) print(“Original DataFrame”) …

Pandas Example – Write a Pandas program to convert a given list of lists into a Dataframe

(Python Example for Beginners)   Write a Pandas program to convert a given list of lists into a Dataframe.   Sample Solution : Python Code : import pandas as pd my_lists = [[‘col1’, ‘col2’], [2, 4], [1, 3]] headers = my_lists.pop(0) print(“Original list of lists:”) print(my_lists) df = pd.DataFrame(my_lists, columns = headers) print(“New DataFrame”) print(df) Sample …

Pandas Example – Write a Pandas program to remove infinite values from a given DataFrame

(Python Example for Beginners)   Write a Pandas program to remove infinite values from a given DataFrame.   Sample Solution : Python Code : import pandas as pd import numpy as np df = pd.DataFrame([1000, 2000, 3000, -4000, np.inf, -np.inf]) print(“Original DataFrame:”) print(df) print(“Removing infinite values:”) df = df.replace([np.inf, -np.inf], np.nan) print(df) Sample Output: Original DataFrame: …