Tag Archives: python for business analysts

Pandas Example – Write a Pandas program to extract a single row, rows and a specific value from a MultiIndex levels DataFrame

(Python Example for Beginners)   Write a Pandas program to extract a single row, rows and a specific value from a MultiIndex levels DataFrame.   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

(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’, ‘city2’]] sales_tuples …

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

(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 t3 3 …

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 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 convert index in a column of the given dataframe

(Python Example for Beginners)   Write a Pandas program to convert index in a column of the given dataframe.   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 count city wise number of people from a given of data set (city, name of the person)

(Python Example for Beginners)   Write a Pandas program to count city wise number of people from a given of data set (city, name of the person). Sample data: city Number of people 0 California 4 1 Georgia 2 2 Los Angeles 4   Sample Solution : Python Code : import pandas as pd df1 = …

Pandas Example – Write a Pandas program to iterate over rows in a DataFrame

(Python Example for Beginners)   Write a Pandas program to iterate over rows in a DataFrame. Sample Python dictionary data and list labels: exam_data = [{‘name’:’Anastasia’, ‘score’:12.5}, {‘name’:’Dima’,’score’:9}, {‘name’:’Katherine’,’score’:16.5}]   Sample Solution : Python Code : import pandas as pd import numpy as np exam_data = [{‘name’:’Anastasia’, ‘score’:12.5}, {‘name’:’Dima’,’score’:9}, {‘name’:’Katherine’,’score’:16.5}] df = pd.DataFrame(exam_data) for index, row …