Practical Xii 11-25
Practical Xii 11-25
Practical Xii 11-25
------------------------------------------------------------------------------
8/10/21
Q12. Create two dataframes using the following two Dictionaries. Merge the two dataframes and append the
second dataframe as a new column to the first dataframe on the basis of the manufacturing company’s name.
import pandas as pd
Car_Price = {'Company': ['Toyota', 'Honda', 'BMW', 'Audi'],
'Price': [23845, 17995, 135925 , 71400]}
carPriceDf = pd.DataFrame(Car_Price)
print(carPriceDf)
print()
carsHorsepowerDf = pd.DataFrame(car_Horsepower)
------------------------------------------------------------------------------
9/10/21
Q13. How to combine many series to form a dataframe?
import numpy as np
import pandas as pd
ser1 = pd.Series(list('abcedf'))
print("series 1")
print(ser1)
print( )
ser2 = pd.Series(np.arange(6))
print("series 2")
print(ser2)
print( )
df1 = pd.concat([ser1, ser2], axis=1)
print("dataframe")
print(df1)
print( )
# or
print("other way to combine series")
print( )
df = pd.DataFrame({'col1': ser1, 'col2': ser2})
print(df)
----------------------------------------------------------------------------------------------------------------------------------
18/10/21
Q14. Write a Program to enter data and show data in python using dataFrames and pandas.
import pandas as pd
data = [['Rajiv',10],['Sameer',12],['Kapil',13]]
df = pd.DataFrame(data,columns=['Name','Age'])
print (df)
print ()
print ( )
data1 = {'Name':['Rajiv', 'Sameer', 'Kapil', 'Nischay'],
'Age':[28,34,29,42],
'Designation':['Accountant','Cashier','Clerk','Manager']}
df1 = pd.DataFrame(data1)
print (df1)
----------------------------------------------------------------------------------------------------------
21/10/21
practical 15
create a series storing population in four cities and the average income of the people in those
cities. Find out the percapita income in four cities.
import pandas as pd
population=pd.Series(data=[100,200,300,400],\
index=['delhi','mumbai','pune','patna']
)
avgincome=pd.Series(data=[10,20,30,40],\
index=['delhi','mumbai','pune','patna'])
percapitainc=avgincome/population
output
create a series display its values, sort the series. Find out the maximum and minimum three
numbers from the series.
import pandas as pd
s1=pd.Series([23,56,11,78,56,34,89,10])
print ("Original Series")
print(s1)
output
Original Series
0 23
1 56
2 11
3 78
4 56
5 34
6 89
7 10
dtype: int64
Sorted Series
7 10
2 11
0 23
5 34
1 56
4 56
3 78
6 89
dtype: int64
Maximum three numbers
4 56
3 78
6 89
dtype: int64
Minimum three numbers
7 10
2 11
0 23
dtype: int64
---------------------------------------------------------------
23/10/21
practical 17
import pandas as pd
import numpy as np
a=np.arange(2,15,3)
print("Array elements are")
print(a)
s=pd.Series(a)
print("Series created by using array elements" )
print(s)
d=pd.Series({'jan':31,'feb':28,'mar':31})
print("Series created by using dictionary" )
print(d)
output-
------------------------------------------------------------------------
24/10/21
practical 18.
Create a series and find out all the elements greater than 75
import pandas as pd
import numpy as np
a=np.arange(50,100,4)
print("Array elements are")
print(a)
s=pd.Series(a)
print("Series created by using array elements" )
print(s)
output
Array elements are
[50 54 58 62 66 70 74 78 82 86 90 94 98]
Series created by using array elements
0 50
1 54
2 58
3 62
4 66
5 70
6 74
7 78
8 82
9 86
10 90
11 94
12 98
dtype: int32
Series values > 75
7 78
8 82
9 86
10 90
11 94
12 98
dtype: int32
---------------------------------------------------------------------------------
25/10/21
Q19. Create a panda’s series from a dictionary of values.
import pandas as pd
dictionary = {'A' : 10, 'B' : 20, 'C' : 30}
series = pd.Series(dictionary)
print(series)
output
A 10
B 20
C 30
dtype: int64
c) Index list is passed of same length as the number of keys present in dictionary.
import pandas as pd
dictionary = {'A' : 50, 'B' : 10, 'C' : 80}
series = pd.Series(dictionary, index =['B', 'C', 'A'])
print(series)
Output:
B 10
C 80
A 50
dtype: int64
d) Index list is passed of greater length than the number of keys present in
dictionary in this case, Index order is persisted and the missing element is
filled with NaN (Not a Number).
import pandas as pd
dictionary = {'A' : 50, 'B' : 10, 'C' : 80}
series = pd.Series(dictionary, index =['B', 'C', 'D', 'A'])
print(series)
Output:
B 10
C 80
D NaN
A 50
dtype: float64
------------------------------------------------------------
26/10/21
Q20. Create a panda’s series from a ndarray and list
a) Series from ndarray
import pandas as pd
import numpy as np
arr = np.array(['S','C','H','O','O','L'])
s = pd.Series(arr)
print(s)
Output :
0 S
1 C
2 H
3 O
4 O
5 L
dtype: object
output-
0 Python
1 for
2 Fun
dtype: object
import pandas as pd
ind = [10, 20, 30, 40, 50, 60, 70]
lst = ['write', 'program', 'for', 'displaying',
'coloured', 'bar', 'plot']
x = pd.Series(lst, index = ind)
print(x)
output-
10 write
20 program
30 for
40 displaying
50 coloured
60 bar
70 plot
dtype: object
------------------------------------------------------
28/10/21
Q21. Write a python program to sort the given series
import pandas as pd
s = pd.Series(['100', '200', 'python', '300.12', '400'])
print("Original Data Series:")
print(s)
news = pd.Series(s).sort_values()
print(news)
output-
examdata= { 'name':['Anas', 'Dim', 'Kat', 'Jam', 'Emil', 'Mich', 'Mat', 'Lau', 'Kev', 'Jon'],
'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 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(examdata , index=labels)
print("Select specific columns:")
print(df[['name', 'score']])
output-
Select specific columns:
name score
a Anas 12.5
b Dim 9.0
c Kat 16.5
d Jam NaN
e Emil 9.0
f Mich 20.0
g Mat 14.5
h Lau NaN
i Kev 8.0
j Jon 19.0
----------------------------
1/11/21
Q24. Write a Pandas program to select the rows where the number of attempts in
the examination is greater than 2.
import pandas as pd
import numpy as np
exam_data = { 'name': ['Ana', 'Dim', 'Kat', 'Jam', 'Emi', 'Mic', 'Matt, 'Lau', 'Kev', 'Jon'],
'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 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 [df['attempts'] > 2])
output-
Number of attempts in the examination is greater than 2:
name score attempts qualify
b Dim 9.0 3 no
d Jam NaN 3 no
f Mic 20.0 3 yes
-------------------------------------------------------------------------------------------------------
1/11/21
x = [1,2,3,4,5,6]
y = [2,4,1,5,2,6]
plt.plot(x, y, color='green', linestyle='dashed', linewidth = 3,
marker='o', markerfacecolor='blue', markersize=12)
plt.ylim(1,8)
plt.xlim(1,8)
plt.xlabel('x - axis')
plt.ylabel('y - axis')
plt.title(‘line chart')
plt.show()