0% found this document useful (0 votes)
57 views18 pages

Info Programs Questions

The document contains 20 questions related to creating and manipulating pandas series and dataframes. The questions cover topics like creating series from lists, dictionaries, ndarrays; selecting rows and columns from dataframes; plotting line graphs, bar graphs, histograms from dataframe data; and inserting, replacing values in dataframes.

Uploaded by

pat
Copyright
© © All Rights Reserved
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% found this document useful (0 votes)
57 views18 pages

Info Programs Questions

The document contains 20 questions related to creating and manipulating pandas series and dataframes. The questions cover topics like creating series from lists, dictionaries, ndarrays; selecting rows and columns from dataframes; plotting line graphs, bar graphs, histograms from dataframe data; and inserting, replacing values in dataframes.

Uploaded by

pat
Copyright
© © All Rights Reserved
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/ 18

Programs/Questions

1. Write a program in python to create a series called countries with data of 5 GCC
countries.
Input:
import pandas as pd
countries=pd.Series([“Kuwait”,”Saudi”,”Dubai”,”Bahrain”,”Iran”])
print(countries)
Output:

2. Create a series from ndarray with index


Input:
import pandas as pd
import numpy as np
data=np.array(['a','b','c','d'])
s=pd.Series(data,index=[100,101,102,103])
print(s)
Output:

3. Create a series by using a python program named ‘Directory’ with telephone


numbers as values. Use the concept of dictionary.
Input:
import pandas as pd
d1={'a':95783442,'b':64749874,'c':25429950,'d':6762049}
directory=pd.Series(d1)
print(directory)

Output:

4. Write a program in python to create a series with the scalar value 8 to be


repeated five times

Input:
import pandas as pd
s=pd.Series(8,index=[0,1,2,3,4])
print(s)

Output:

5. Create a series of capitals of countries and display all the attributes of series
i) .name

Input:
import pandas as pd
CapCntry={'India':'New Delhi','USA':'Washington
DC','UK':'London','France':'Paris'}
CapCntry=pd.Series(CapCntry,name='Capitals')
print(CapCntry)

Output:
ii) .index.name

Input:
import pandas as pd
seriesCapCntry=pd.Series(['New Delhi','Washington
DC','London','Paris'],index=['India','USA', 'UK','France'])
seriesCapCntry.name=('Capitals')
seriesCapCntry.index.name='Countries'
print(seriesCapCntry)

Output:

iii) .values

Input:
import pandas as pd
seriesCapCntry=pd.Series(['New Delhi','Washington
DC','London','Paris'],index=['India','USA', 'UK','France'])
print(seriesCapCntry.values)

Output:

iv) .size

Input:
import pandas as pd
seriesCapCntry=pd.Series(['New Delhi','Washington
DC','London','Paris'],index=['India','USA', 'UK','France'])
print(seriesCapCntry.size)

Output:

6. Write a program in python to find the difference, product, quotient of the given
below series using operators
A=pd.Series([10,11,12,13,14],index=['a’,’b’,’c’,’d’,’e’])
B=pd.Series([10,11,12,13,14],index=['a’,’b’,’d’,’e’,’f’])

i) For subtraction

Input:
import pandas as pd
A=pd.Series([10,11,12,13,14],index=["a","b","c","d","e"])
B=pd.Series([10,11,12,13,14],index=["a","b","d","e","f"])
C=A.sub(B)
print(C)

Output:

ii) For multiplication

Input:
import pandas as pd
A=pd.Series([10,11,12,13,14],index=["a","b","c","d","e"])
B=pd.Series([10,11,12,13,14],index=["a","b","d","e","f"])
C=A.mul(B)
print(C)

Output:

iii) For division

Input:
import pandas as pd
A=pd.Series([10,11,12,13,14],index=["a","b","c","d","e"])
B=pd.Series([10,11,12,13,14],index=["a","b","d","e","f"])
C=A.div(B)
print(C)

Output:

7.Write a program in python to find the difference of the given below series using
fill_value = 2

A=pd.Series([10,11,12,13],index=["a","b","c","d"])
B=pd.Series([1,2,3,4],index=["x","y","a","b"])

Input:
import pandas as pd
A=pd.Series([10,11,12,13],index=["a","b","c","d"])
B=pd.Series([1,2,3,4],index=["x","y","a","b"])
C=A.sub(B,fill_value=2)
print(C)

Output:

8. Display number of rows and columns in a dataframe

Input:
import pandas as pd
exam_data={'Name':['Anastasia','Dima','Katherine'],'Score':[12.5,9,16.5],'Attempt':
[1,3,2],'Qualify':['yes','no','yes']}
labels =['a','b','c']
df=pd.DataFrame(exam_data,index=labels)
print(df)
total_rows=len(df.axes[0])
total_columns=len(df.axes[1])
print('Total rows:',total_rows)
print('Total colums:',total_columns)

Output:
9. Create a dataframe for examination result and display row labels, column labels
and datatypes of each column and the dimensions.

Input:
import pandas as pd
dic={'Class':['I','II','III','IV','V','VI','VII','VIII','IX','X','XI','XII'],'pass-Percentage':
[100,100,100,100,100,100,100,100,100,98.6,100,99]}
result=pd.DataFrame(dic)
print(result)
print(result.dtypes)
print('shape of the dataframe is:')
print(result.shape)
print(result.info())

Output:
10. Write a python program to select the rows where number of attempts in the
examination are greater than 2.

Input:
import pandas as pd
import numpy as np
exam_data={'Names':
['Anastasia','Dima','Katherine','James','Emily','Micheal','Mathew','Laura','Kevin','ja
mes'],'Scores':[12.5,9,16.5,np.nan,9,20,14.5,np.nan,9,19],'Attempts':
[1,3,2,3,2,3,1,1,2,1],'qualify':['yes','no','yes','no','no','yes','yes','no','no','yes']}
labels=['a','b','c','d','e','f','g','h','i','j']
df=pd.DataFrame(exam_data,index=labels)
print('Number of Attempts in the examination is greater than 2:')
print(df)
print(df[‘Attempts’]>2)
print(df[df['Attempts']>2])

Output:
11. Write a program to add one row in an existing dataframe.

Input:
import pandas as pd
d={'col1':[0,1,2,3,4],'col2':[14,45,36,47,58],'col3':[7,8,9,6,1]}
df=pd.DataFrame(d)
print('Original data')
print(df)

Output:

12. Create a DataFrame named ‘Airways’ and solve the questions below:

Input:
import pandas as pd
data={'Airline':['Air India','Kuwait Airways','Jet Airways','Indigo','Air Arabia'],
'Year':['2018','2019','2020','2021','2022'], 'Month': ['Jan','Feb','Mar','Apr','May'],
'Passengers':[25,30,45,67,56]}
Airways=pd.DataFrame(data)
print(Airways)

Output:

a) Write a command in python to extract data from ‘Air India’ to ‘Indigo’ for all
columns
Input:
print(Airways.loc['Air India':'Indigo','Year':'Passenger'])

Output:

b) Write a command in python to extract details of ‘Indigo’ flight

Input:
print(Airways.loc['Indigo'])

Output:

c) Write a command to extract the number of passengers in all flights

Input:
print(Airways.loc[:,'Passenger'])

Output :

d) Write a command to extract all details for all flights


Input:
print(Airways.loc[:,:])
Output:
e) Write a command to extract year and passenger number of all flights in month
Jan
Input:
print(Airways[['Year','Passenger']][Airways['Month']=='Jan'])
Output:

f) Write a command to extract Kuwait Airways number of passengers using ‘at’


function
Input:
print(Airways.at['Kuwait Airways','Passenger'])
Output:

13. Replace all non-numeric values with 0 in a dataframe

Input:
import pandas as pd
import numpy as np
exam_data={'Names':
['Anastasia','Dima','Katherine','James','Emily','Micheal','Mathew','Laura','Kevin','ja
mes'],'Scores':[12.5,9,16.5,np.nan,9,20,14.5,np.nan,np.nan,19],'Attempts':
[1,3,2,3,2,3,1,1,2,1],'qualify':['yes','no','yes','no','no','yes','yes','yes','yes','no']}
labels=['a','b','c','d','e','f','g','h','i','j']
df=pd.DataFrame(exam_data,index=labels)
print(df)
df=df.fillna(0)
print('\nNew DataFrame replacing all NaN with 0')
print(df)

Output:
14. Insert a specific column at a given index.

Input:
import pandas as pd
d={'col2':[4,5,6,9,5],'col3':[7,8,12,1,11]}
df=pd.DataFrame(data=d)
print('Original DataFrame:')
print(df)
new_col=[1,2,3,4,7]
idx=0
df.insert(loc=idx,column='col1',value=new_col)
print('\nNew DataFrame:')
print(df)
Output

15. Draw a line graph representing the percentage of marks for each person.
A) Set the appropriate label in x-axis and y-axis.
B) Set the title of the graph “Percentage Comparison”

Input:
import matplotlib.pyplot as plt
x_values = ['Anu','Hari','Riya']
y_values = [40,50,45]
plt.plot(x_values, y_values, linewidth='5', ls='dashdot', color='r')
plt.xlabel('Names')
plt.ylabel('Marks')
plt.title('Percentage of Marks')
plt.savefig('percentagecomparison_trial')
plt.show()

Output:
16. Write a program to draw the line graph .
I) Set the appropriate label to x-axis and y-axis.
Ii) Set the title of the graph “Number of Students”.
Iii) Show the grid lines.

Input:
import matplotlib.pyplot as plt
x=[1.00,2.00,3.00]
y=[4.0,5.0,1.0]
plt.plot(x,y,linewidth='1',color='b')
plt.xlabel('x value')
plt.ylabel('y value')
plt.title('Number of students')
plt.savefig('numberofstudents_trial')
plt.grid()
plt.show()

Output:
17. Write a program to draw a bar graph.

Input:
import matplotlib.pyplot as plt
import numpy as np
Names=['Amit','Mohan','Sudha']
X=np.arange(len(Names))
Maths=[100,95,85]
Science=[100,50,90]
SST=[60,57.48,53.58]
plt.bar(X,Maths,color='red',width=.25,label='Mat')
plt.bar(X+.25,Science,color='yellow',width=.25,label='Sci')
plt.bar(X+.50,SST,color='green',width=.25,label='SST')
plt.title('Result Analysis',fontsize=15)
plt.xlabel('Names',fontsize=15)
plt.ylabel('Names',fontsize=15)
plt.legend()
plt.show()

Output:
18. Draw a line graph.

Input:
import matplotlib.pyplot as plt
import numpy as np
Names=['Amit','Mohan','Sudha']
X=np.arange(len(Names))
Maths=[100,95,85]
Science=[100,50,90]
SST=[60,57.48,53.58]
plt.plot(Names,Maths,color='red',ls='dashdot',marker='*',linewidth=1.5,label='Mat'
)
plt.plot(Names,Science,color='blue',ls='dashed',marker='o',linewidth=1.5,label='Sc
i')
plt.plot(Names,SST,color='green',ls='dashdot',marker='D',linewidth=1.5,label='SS
T')
plt.title('Result Analysis',fontsize=15)
plt.xlabel('Names',fontsize=15)
plt.ylabel('Scores',fontsize=15)
plt.legend()
plt.show()
Output:

19 Draw a histogram.

Input:
import matplotlib.pyplot as plt
import numpy as np
data=[1,11,21,31,41]
plt.hist([5,15,25,35,45,55],bins=[0,10,20,30,40,50,60],weights=[20,10,45,33,6,8],e
dgecolor ='red')
plt.show()

Output:
20. Draw a histogram using horizontal orientation

Input:

import matplotlib.pyplot as plt


data=[1,11,21,31,41,51,61]
plt.hist(data,bins=7,weights=[0,10,20,30,40,50,60],edgecolor='red',orientation='hor
izontal')
plt.show()

Output:

You might also like