You are on page 1of 14

7/10/21

Q11. Write a program to explain Boolean indexing.

#boolean indexing using dataframe


import pandas as pd
d1={'rollno': [10,11,12,13,14,15],
'name':['aniket','ram','shyam','bhim','rim','kim']}
df=pd.DataFrame(d1,index=[True,False,True,True,False,True])
print(df)
print(df.loc[True])
print()
print("iloc method displays the output as :" )
print(df.iloc[1])

------------------------------------------------------------------------------
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()

car_Horsepower = {'Company': ['Toyota', 'Honda', 'BMW', 'Audi'],


'horsepower': [141, 80, 182, 160]}

carsHorsepowerDf = pd.DataFrame(car_Horsepower)

carsDf = pd.merge(carPriceDf, carsHorsepowerDf, on="Company")


print(carsHorsepowerDf)
print()
print(carsDf)

------------------------------------------------------------------------------
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

print("Population in four cities")


print(population)

print("average income in four cities")


print(avgincome)

print(" Per Capita income in four cities")


print(percapitainc)

output

Population in four cities


delhi 100
mumbai 200
pune 300
patna 400
dtype: int64
average income in four cities
delhi 10
mumbai 20
pune 30
patna 40
dtype: int64

Per Capita income in four cities


delhi 0.1
mumbai 0.1
pune 0.1
patna 0.1
dtype: float64
---------------------------------------------------------------
22/10/21
practical 16

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)

print(" Sorted Series")


print(s1.sort_values( ))

print("Maximum three numbers")


print(s1.sort_values( ).tail(3))

print("Minimum three numbers")


print(s1.sort_values( ).head(3))

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

Create a series by using array elements and dictionary

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-

Array elements are


[ 2 5 8 11 14]
Series created by using array elements
0 2
1 5
2 8
3 11
4 14
dtype: int32
Series created by using dictionary
jan 31
feb 28
mar 31
dtype: int64

------------------------------------------------------------------------
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)

print("Series values > 75" )


print(s[s>75])

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.

a) Dictionary keys are given in sorted order

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

b) Dictionary keys are given in unsorted order.


import pandas as pd
dictionary = {'D' : 10, 'B' : 20, 'C' : 30}
series = pd.Series(dictionary)
print(series)
output
B 20
C 30
D 10
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

b)series from list-


1) Using Series() method without any argument.
import pandas as pd
# create Pandas Series with default index values
# default index ranges is from 0 to len(list) - 1
x = pd.Series(['Python', 'for', 'Fun'])
print(x)

output-
0 Python
1 for
2 Fun
dtype: object

2) Using Series() method with 'index' argument.


import pandas as pd
x = pd.Series([10, 20, 30, 40, 50], index =['a', 'b', 'c', 'd', 'e'])
print(x)
output-
a 10
b 20
c 30
d 40
e 50
dtype: int64
3) Using Series() method with 'index' argument.

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-

Original Data Series:


0 100
1 200
2 python
3 300.12
4 400
dtype: object
0 100
1 200
3 300.12
4 400
2 python
dtype: object
------------------------------------------------------
29/10/21
Q22. Write a program to print all the elements that satisfy the condition s<6.
import pandas as pd
s = pd.Series([0,1,2,3,4,5,6,7,8,9,10])
print("Original Data Series:")
print(s)
print("\Elements satisfying the condition:")
n=6
news = s[s < n]
print(news)
output-
Original Data Series:
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
dtype: int64

Elements satisfying the condition:


0 0
1 1
2 2
3 3
4 4
5 5
dtype: int64
-----------------------------------------
30/10/21
Q23. Write a program to select the 'name' and 'score' columns from the DataFrame.
import pandas as pd
import numpy as np

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

Q25. To plot a line chart with markers

import matplotlib.pyplot as plt

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()

You might also like