Tag Archives: python for data analyst

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 get column index from column name of a given DataFrame

(Python Example for Beginners)   Write a Pandas program to get column index from column name of a given 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 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: …

Pandas Example – Write a Pandas program to convert the datatype of a given column(float to int)

(Python Example for Beginners)   Write a Pandas program to convert the datatype of a given column(float to int).   Sample Solution : Python Code : import pandas as pd import numpy as np exam_data = {‘name’: [‘Anastasia’, ‘Dima’, ‘Katherine’, ‘James’, ‘Emily’, ‘Michael’, ‘Matthew’, ‘Laura’, ‘Kevin’, ‘Jonas’], ‘score’: [12.5, 9.1, 16.5, 12.77, 9.21, 20.22, 14.5, 11.34, …

Pandas Example – Write a Pandas program to sort a given DataFrame by two or more columns

(Python Example for Beginners)   Write a Pandas program to sort a given DataFrame by two or more columns.   Sample Solution : Python Code : import pandas as pd import numpy as np exam_data = {‘name’: [‘Anastasia’, ‘Dima’, ‘Katherine’, ‘James’, ‘Emily’, ‘Michael’, ‘Matthew’, ‘Laura’, ‘Kevin’, ‘Jonas’], ‘score’: [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, …

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 append data to an empty DataFrame. Sample data: Original DataFrame: After appending some data: col1 col2 0 0 0 1 1 1 2 2 2   Sample Solution : Python Code : import pandas as pd import numpy as np df = pd.DataFrame() data = …