Data Structures in Python Pandas Assignment

Rumman Ansari   Software Engineer   2021-01-21   8914 Share
☰ Table of Contents

Table of Content:


1. Python Pandas | 1 | Data Structures in Pandas

Task 1

  • Create a series named heights_A with values 176.2, 158.4, 167.6, 156.2, and 161.4. These values represent the height of 5 students of class A.

  • Label each student as s1, s2, s3, s4, and s5.

  • Determine the shape of heights_A and display it.

Note: Use the Series method available in the pandas library.


#import the pandas library and aliasing as pd
import pandas as pd
# Creating the Series
heights_A = pd.Series([ 176.2, 158.4, 167.6, 156.2, 161.4 ])

# Creating the row axis labels 
heights_A.index = ['s1', 's2', 's3', 's4','s5']

# return the shape
heights_A.shape

# Print the series
print (heights_A)

Task 2

- Create another series named weights_A with values 85.1, 90.2, 76.8, 80.4, and 78.9. These values represent the weights of 5 students of class A.

- Label each student as s1, s2, s3, s4, and s5.

- Determine data type of values in weights_A and display it.

 

  • Note: Use the Series method available in the pandas library.


#import the pandas library and aliasing as pd
import pandas as pd
# Creating the Series
weights_A = pd.Series([85.1, 90.2, 76.8, 80.4 , 78.9])

# Creating the row axis labels
weights_A.index = ['s1', 's2', 's3', 's4','s5']

# Determine data type 
weights_A.dtypes

# Print the series
print (weights_A)

 

Task 3

- Create a dataframe named df_A, which contains the height and weight of five students namely s1, s2, s3, s4 and s5.

- Label the columns as Student_height and Student_weight, respectively.

- Display the shape of df_A.

  Note: Use the DataFrame method in pandas, and also the series heights_A, weights_A created in the previous problems.


#import the pandas library and aliasing as pd
import pandas as pd

# Creating the Series
heights_A = pd.Series([ 176.2, 158.4, 167.6, 156.2, 161.4 ])

# Creating the row axis labels 
heights_A.index = ['s1', 's2', 's3', 's4','s5']

# Creating the Series
weights_A = pd.Series([85.1, 90.2, 76.8, 80.4 , 78.9])

# Creating the row axis labels
weights_A.index = ['s1', 's2', 's3', 's4','s5']
 
df_A = pd.DataFrame()

df_A['Student_height'] = heights_A

df_A['Student_weight'] = weights_A   

# Display the shape of dataframe df_A
df_A.shape

# Print the dataframe
print (df_A)

 

Task 4

- Create another series named heights_B from a 1-D numpy array of 5 elements derived from the normal distribution of mean 170.0 and standard deviation 25.0.

   Note: Set random seed to 100 before creating the heights_B series.

- Create another series named weights_B from a 1-D numpy array of 5 elements derived from the normal distribution of mean 75.0 and standard deviation 12.0.

    Note: Set random seed to 100 before creating the weights_B series.

- Label both series elements as s1, s2, s3, s4 and s5.
 
- Print the mean of series heights_B.

 

 

Task 5

    - Create a dataframe df_B containing the height and weight of students s1, s2, s3, s4 and s5 belonging to class B.

    - Label the columns as Student_height and Student_weight respectively.

    - Display the column names of df_B.

      Note: Use the heights_B and weights_B series created in the above tasks.

 

 

Task 6

- Create a panel p, containing the previously created two dataframes df_A and df_B.

- Label the first dataframe as ClassA, and second as ClassB.

- Determine the shape of panel p and display it.

Note: Use the Panel method of pandas.