Frescoplay HandsOn Python Pandas Working With CSV's
Task 1
•
Create a series
named heights_A with
values 176.2, 158.4, 167.6, 156.2, and 161.4. These values represent heights
of 5 students of class A.
• Label each student
as s1, s2, s3, s4, and s5.
•
Create another series named weights_A with values
85.1, 90.2, 76.8, 80.4, and 78.9. These values
represent weights of 5 students
of class A.
• Label each student
as s1, s2, s3, s4, and s5.
•
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.
•
Write the contents of df_A to a CSV file
named classA.csv.
Note:Use the to_csv method associated with a data frame.
•
Verify if the file classA.csv exists in
the present directory using command
Is -I.
• You
can also view the contents
of the file using the command cat
classA.csv
Click on Run then Install then write the below
code in prog file. Then click Run then Run.
#Write your
code here import numpy as np import pandas as pd
height_A=pd.Series([176.2, 158.4, 167.6, 156.2,161.4], index = ['s1', 's2', 's3', 's4', 's5'])
weight_A=pd.Series([85.1, 90.2, 76.8, 80.4, 78.9], index = ['s1', 's2', 's3', 's4', 's5']) df_A=pd.DataFrame({'Student_height':height_A, 'Student_weight':weight_A})
df_A.to_csv("classA.csv") df_A2=pd.read_csv("classA.csv") print(df_A2)
df_A3=pd.read_csv("classA.csv", index_col=0) print(df_A3)
np.random.seed(100)
heights_B = pd.Series(np.random.normal(loc= 170.0, scale=25.0, size=5)) np.random.seed(100)
weights_B=pd.Series(np.random.normal(loc= 75.0, scale=12.0, size=5))
df_B=pd.DataFrame({'Student_height':heights_B, 'Student_weight':weights_B}) df_B.to_csv("classB.csv", index=False)
df_B2=pd.read_csv("classB.csv")
print(df_B2)
df_B3=pd.read_csv("classB.csv", header=None) print(df_B3)
df_B4=pd.read_csv("classB.csv", header=None,skiprows=2) print(df_B4)
Run Test: Will be succesful
Disclaimer:This
site is for pure educational purpose only.we recommend it only for reference.we
still encourage to go through the course and learn the topics
Post a Comment