Info Programs Questions
Info 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:
Output:
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:
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:
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:
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:
Input:
print(Airways.loc['Indigo'])
Output:
Input:
print(Airways.loc[:,'Passenger'])
Output :
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:
Output: