The document contains code snippets and output related to pandas series and dataframe operations. It includes questions that create series objects with random integers and custom indexes, reindex a series, sort a series based on indexes, create a series from user input and calculate cubes, create a series storing a numeric table, create a dataframe with salesman details, and create a multi-indexed dataframe with yearly sales figures for different salespeople. The last question demonstrates various methods to access and manipulate the sales dataframe, such as displaying row/column labels, data types, dimensions, and selecting specific rows/columns.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
26 views
Krish Rawal Project
The document contains code snippets and output related to pandas series and dataframe operations. It includes questions that create series objects with random integers and custom indexes, reindex a series, sort a series based on indexes, create a series from user input and calculate cubes, create a series storing a numeric table, create a dataframe with salesman details, and create a multi-indexed dataframe with yearly sales figures for different salespeople. The last question demonstrates various methods to access and manipulate the sales dataframe, such as displaying row/column labels, data types, dimensions, and selecting specific rows/columns.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10
Journal File Krish P Rawal
Q1.Write a program to create a series object with 6 random integers
and having indexes as: [‘p’,’q’,’r’,’n’,’t’,’v’]. Code: Import pandas as pd Import numpy as np S=pd.Series(np.random.randint(6,size=6),index=[‘p’,\ ‘q’,’r’,’n’,’t’,’v’]) print(s) Output: p 4 q 4 r 4 n 4 t 4 v 4 dtype:int64
065-Informatic Practices Pg. 1
Journal File Krish P Rawal
Q2.Write a program to create a series and then change the indexes of
the series object in any random order Code: import pandas as pd import numpy as np s1=pd.Series(data=[100,200,300,400,500],indexes=[‘I’\ ,’J’,’K’,’L’,’M’]) print(‘Original Data Series:’) print(s1) s1=s1.reindex(index=[‘K’,’I’,’M’,’L’,’J’]) print(‘Data Series after changing the index:’) print(s1) Output: Original Data Series: I 100 J 200 K 300 L 400 M 500 dtype:int64 Data Series after changing the index: K 300 I 100 M 500 L 400 J 200 dtype:int64
065-Informatic Practices Pg. 2
Journal File Krish P Rawal
Q3.Write a program to sort the values of a Series object s1 in
descending order of its indexes and store it into Series object s3 Code: import pandas as pd s1=pd.Series(data=[6700,5600,5000,5200],index=[1,2,3\ ,4]) s3=s1.sort_index(ascending=False) print(‘Series object s1:’) print(s1) print(‘Series object s3:’) print(s3) Output: Series object s1: 1 6700 2 5600 3 5000 4 5200 dtype:int64 4 5200 3 5000 2 5600 1 6700 dtype:int64
065-Informatic Practices Pg. 3
Journal File Krish P Rawal
Q4.Write a program to create a 3-size series using input() and
calculate the cube of the series value. Code: import pandas as pd n=eval(input(“Enter any 3 numbers of your choice:”)) s=pd.Series(n) print(“Series object s:”) print(s4) print(“Cubes of s values:”) print(s**3) Output: Enter any 3 number of your choice:5,6,7 Series object s: 0 5 1 6 2 7 dtype:int64 Cubes of s values: 0 125 1 216 2 343 dtype:int64
065-Informatic Practices Pg. 4
Journal File Krish P Rawal
065-Informatic Practices Pg. 5
Journal File Krish P Rawal
Q5.Write a program to create series object that stores the table of 7.
Q6.Write a program to create a Dataframe storing salesman details
(name,zone,sales) of 5 salesmen. Code: import pandas as pd n={‘Name’:[‘Raj’,’Kunal’,’Sam’,’Joy’,’Shiv’], ‘Sales’:[1000,2000,4000,8000,16000]} i=[‘Zone1’,’Zone2’,’Zone3’,’Zone4’,’Zone5’] df=pd.DataFrame(n,i) print(“DataFrame containing salesman details:”) print(df) Output: DataFrame containing salesman details: Name Sales Zone1 Raj 1000 Zone2 Kunal 2000 Zone3 Sam 4000 Zone4 Joy 8000 Zone5 Shiv 16000
065-Informatic Practices Pg. 7
Journal File Krish P Rawal
Q7.Create the following Dataframe Sales containing year-wise sales
figures for five sales persons in INR.Use the years as column labels,and salesperson names as row labels. 2014 2015 2016 2017 Madhu 100.5 12000 20000 50000 Kusum 105.8 18000 50000 60000 Kinshuk 200.9 22000 70000 70000 Ankit 30000 30000 100000 80000 Shruti 40000 45000 125000 90000 Code: import pandas as pd saledict={2014: {‘Madhu’:100.5,’Kusum’:150.8,’Kinshuk’:200.9,’Ankit’: 30000,’Shruti’:40000}, 2015:{‘Madhu’:12000,’Kusum’:18000,’Kinshuk’:22000,\ ’Ankit’:30000,’Shruti’:45000}, 2016:{‘Madhu’:20000,’Kusum’:50000,’Kinshuk’:70000,\ ‘Ankit’:100000,’Shruti’:125000}, 2017:{‘Madhu’:50000,’Kusum’:60000,’Kinshuk’:70000,\ ‘Ankit’:80000,’Shruti’:90000}} sales=pd.DataFrame(saledict) print(sales)
065-Informatic Practices Pg. 8
Journal File Krish P Rawal
Q8.Use the DataFrame created in Question 7 above to do the
following: a) Display the row labels of Sales. b) Display the column labels of Sales. c) Display the data types of each column of Sales. d) Display the dimensions,shape,size and values of Sales. e) Display the last two rows of Sales. f) Display the first two column of Sales. Code: a)sales.index b)sales.columns c)sales.dtypes d)sales.ndim,sales.shape,sales.size,sales.values e)sales.iloc[3:,] f)sales.iloc[:,:2] Output: a)Index([‘Madhu’,’Kusum’,’Kinshuk’,’Ankit’,’Shruti’] ,dtype=’object’) b)Index([2014,2015,2016,2017],dtype=’int64’) c) 2014 float64 2015 int64 2016 int64 2017 int64 dtype:object