You are on page 1of 6

QNo Question

Consider the given dataframe:

Help him to create the above DataFrame using a python program.


import pandas as pd
data=[[20,30,5],[40,50,10],[60,70,15],[80,90,25]]
I.a df1=pd.DataFrame(data,index=["D1","D2","D3","D4"],
columns=["F1","F2","F3"])
print(df1)
Write a python statement for getting the output as:

I.b
print(df1.loc[["D1","D3"]][["F1","F3"]])
or
print(df1.loc[['D1', 'D3'],['F1', 'F3']])
or
print(df1[["F1","F3"]].loc[["D1","D3"]])
Write a python statement for getting the output as:

I.c

print(df1.loc[["D4","D3"]][["F3","F2"]])

Gaytri, a data analyst has stored four employee’s name and their employee code in four
dictionaries. Structure of one such dictionary is as follows: Emp1={'Ename': 'Emp Name',
'Ecode’:Employee code}
She clubbed these four dictionary into a list. Write suitable Python code to store the required data
of four employees in the form of list of dictionaries and create a DataFrame with appropriate
column
headings as shown below:

II
import pandas as pd
data=[{'Ename':'John', 'Ecode':88}, {'Ename':'Emily',
'Ecode':92},
{'Ename':'Michael', 'Ecode':78}, {'Ename':'Sophia','Ecode':95}]
df=pd.DataFrame(data)
print(df)
Help Mrs. Gaytri to get the desired output.
Desired output Code
Details of employees with
name “Sophia”
print(df[df["Ename"]=="Sophia"])

print(df.loc[[0,2]])
II.a

Details of employees with


whose Ecode is greater than
90.
print(df[df["Ecode"]>90])

.
Find the output of the following code:
import pandas as pd
lst1=[20,35,40]
ser1=pd.Series([20,35,40])
print(lst1+ lst1)
print(ser1+ser1)
III.a
[20, 35, 40, 20, 35, 40]
0 40
1 70
2 80

Find the output of the following code:


k1=[10,12,15]
m1=[3,5,6]
ser1=pd.Series(k1)
ser2=pd.Series(m1)
ser3=ser1%ser2
III.b print(ser3)

0 1
1 2
2 3
dtype: int64
Write a python program to create the below pandas series.
Yosemite California
Yellowstone Wyoming
Glacier Montana
Rocky Mountain Colorado
dtype: object

import pandas as pd
IV data = {'Yosemite': 'California', 'Yellowstone': 'Wyoming',
'Glacier': 'Montana', 'Rocky Mountain': 'Colorado'}
national_parks = pd.Series(data)
print(national_parks)

Also write a statement to get the required output as "California".


print(national_parks['Yosemite'])

Consider the dataframe given below.

V
Identify the incorrect statements from the following, predict the output of correct statements.
a) print(df.loc[3,"Ecode"])
b) print(df.loc[3]["Ecode"])
c) print(df."Ecode"[3]) #incorrect
d) print(df["Ecode"][3])
e) print(df.Ecode[3])
f) print(df.iloc[3,1])
g) print(df.iloc[3,2]) #incorrect
h) print(df.iloc[3][1])
Consider the given DataFrame 'Employees':

Write suitable Python statements for the following operations:


1. Create the above given DataFrame.
import pandas as pd
VI data={'Name':["Alice","Bob","Carol","David"],
"Employee_ID":["EMP001","EMP002","EMP003","EMP004"],
"Department":["HR","Sales","IT","Marketing"]}
df=pd.DataFrame(data)
print(df)
2. Add a column called 'Salary' with the following data:[55000, 60000, 65000, 58000].
Write suitable Python statements for the following operations:

df["Salary"]=[55000, 60000, 65000, 58000]

3. Include a new employee named 'Eve' with Employee_ID 'EMP005', working in the 'Finance'
department, and a salary of 62000
df.loc[4]=["Eve","EMP005","Finance",62000]

4. Change the name of the 'Employee_ID' column to 'ID'


df.rename({'Employee_ID' :'ID'},axis=1,inplace=True)
df.rename({'Employee_ID' :'ID'},axis="columns",inplace=True)
df.rename(columns={'Employee_ID' :'ID'},inplace=True)

Wizbiz Corporation is recording the quarterly sales data of its three products through different
departments. The data is as follows:

The company stores this information in a CSV file named "Quarterly_Sales.csv". Mr. Raj is
tasked for writing a Python program to visualize this data. He wrote the following Python code
but encountered some difficulties. Help him to write the above visualization program.
✓ Create dataframe, write to csv file, read from csv file, visualize.

import pandas as pd
import matplotlib.pyplot as plt #line 1
data=[[3500, 4200, 4800, 5100],[ 2800, 3100, 3600, 3900],[1500, 1800,
2100, 2400]]

df=pd.DataFrame(data,columns=["Qtr1", "Qtr2" ,"Qtr3", "Qtr4"],


index=[ "Product1","Product2","Product3"])
VII.a df.to_csv("'Quarterly_Sales.csv")
df=pd.read_csv("'Quarterly_Sales.csv",index_col=0) #line 2

df.plot(kind='bar', color=['purple', 'orange', 'green', 'yellow'])


#line 3
plt.title ('Quarterly Sales Report') #line 4
plt.xlabel('Product')
plt.ylabel('Sales')
plt.show()
Write Python statement to display total sales done in 'Qtr1' and 'Qtr2' for each product.

import pandas as pd
import matplotlib.pyplot as plt #line 1
VII.b data=[[3500, 4200, 4800, 5100],[ 2800, 3100, 3600, 3900],[1500, 1800, 2100,
2400]]

df=pd.DataFrame(data,columns=["Qtr1", "Qtr2" ,"Qtr3", "Qtr4"],


index=[ "Product1","Product2","Product3"])
print(df)
print(pd.DataFrame(df["Qtr1"]+df["Qtr2"],columns=["Qtr1+Qtr2"]))

or
print(df["Qtr1"]+df["Qtr2"])

District wise total number of houses are represented in the following table:
40 45 35 44
Dist VII Dist VIII Dist IX Dist X
Draw the following bar graph representing the number of houses in each District(Dist VII,
Dist VIII, Dist IX, Dist X).

VIII

Also, give suitable python statement to save this chart in E: drive of the computer with name
‘house.png’.
import matplotlib.pyplot as plt
district = ['VII','VIII','IX','X']
houses = [40,45,35,44]
plt.bar(district, houses)
plt.savefig('E:\\house.png')
plt.show()
Write a python program to plot a line chart based on the given data to depict the weekly study
patterns for all the seven days.
Day=[1,2,3,4,5,6,7]
Study_Hours=[5,4,6,5,7,8,10]
Also, give suitable python statement to save this chart in d: drive of the computer with name
study.png’.
VIII.b import matplotlib.pyplot as plt
Day=[1,2,3,4,5,6,7]
Study_Hours=[5,4,6,5,7,8,10]
plt.plot(Day,Study_Hours)
plt.savefig('D:\\study.png')
plt.show()

You might also like