Q5.
WAP to create series using dictionary a) store 4 friends'
names (index) and their phone numbers b) modify phone no. of
'riya' c) add one more friend and number d) display phone number
of 'servi' and 'chestha'
Code:
import pandas as pd
friends = {'riya': 9876543210, 'servi': 9123456780, 'chestha': 9988776655, 'arpit': 9012345678}
s = pd.Series(friends)
print("Original:")
print(s)
# b) modify riya
s['riya'] = 9811111111
print("\nAfter modifying riya:")
print(s)
# c) add one more friend
s['neha'] = 9090909090
print("\nAfter adding neha:")
print(s)
# d) display servi & chestha
print("\nPhone numbers of servi and chestha:")
print(s[['servi','chestha']])
Output:
Original:
riya 9876543210
servi 9123456780
chestha 9988776655
arpit 9012345678
dtype: int64
After modifying riya:
riya 9811111111
servi 9123456780
chestha 9988776655
arpit 9012345678
dtype: int64
After adding neha:
riya 9811111111
servi 9123456780
chestha 9988776655
arpit 9012345678
neha 9090909090
dtype: int64
Phone numbers of servi and chestha:
servi 9123456780
chestha 9988776655
dtype: int64
Q6. WAP to create series using list/sequence 11, 21, 31, 41
Code:
import pandas as pd
s = pd.Series([11,21,31,41])
print(s)
Output:
0 11
1 21
2 31
3 41
dtype: int64
Q7. WAP to create series to store 4 names and weight using
dictionary
Code:
import pandas as pd
weights = {'Aman': 62, 'Bhavya': 55, 'Charu': 68, 'Deep': 72}
s = pd.Series(weights)
print(s)
Output:
Aman 62
Bhavya 55
Charu 68
Deep 72
dtype: int64
Q8. WAP to create series to store runs scored by 3 cricket players
(player name as index)
Code:
import pandas as pd
runs = pd.Series([78, 102, 45], index=['Virat', 'Rohit', 'Gill'])
print(runs)
Output:
Virat 78
Rohit 102
Gill 45
dtype: int64
Q9. a) create series using array 11,21,31,41 c) display rows 1 to 3
using iloc d) display rows 1 to 3 using loc (labels) e) display rows
2 and 4
Code:
import pandas as pd
import numpy as np
s = pd.Series(np.array([11,21,31,41]), index=[1,2,3,4])
print("Series:")
print(s)
print("\n(c) iloc 1:3 (positions 1 to 3, exclusive of 3):")
print(s.iloc[1:3])
print("\n(d) loc 1 to 3 (labels 1 through 3):")
print(s.loc[1:3])
print("\n(e) rows 2 and 4:")
print(s.loc[[2,4]])
Output:
Series:
1 11
2 21
3 31
4 41
dtype: int64
(c) iloc 1:3 (positions 1 to 3, exclusive of 3):
2 21
3 31
dtype: int64
(d) loc 1 to 3 (labels 1 through 3):
1 11
2 21
3 31
dtype: int64
(e) rows 2 and 4:
2 21
4 41
dtype: int64
Q10. WAP to ask quarter name and sales; create series for 4
quarters b) display sales of Q1 and Q2 c) display sales of index 3
d) display sales from Q2 to Q4 e) display first element using iloc
Code:
import pandas as pd
sales = pd.Series({'Q1': 2500, 'Q2': 3700, 'Q3': 4200, 'Q4': 3100})
print("All Quarters:")
print(sales)
print("\n(b) Sales of Q1 and Q2:")
print(sales[['Q1','Q2']])
print("\n(c) Sales of index 3 (0-based = 3rd element):")
print(sales.iloc[3])
print("\n(d) Sales from Q2 to Q4:")
print(sales.loc['Q2':'Q4'])
print("\n(e) First element using iloc:")
print(sales.iloc[0])
Output:
All Quarters:
Q1 2500
Q2 3700
Q3 4200
Q4 3100
dtype: int64
(b) Sales of Q1 and Q2:
Q1 2500
Q2 3700
dtype: int64
(c) Sales of index 3 (0-based = 3rd element):
3100
(d) Sales from Q2 to Q4:
Q2 3700
Q3 4200
Q4 3100
dtype: int64
(e) First element using iloc:
2500
Q11. Create 2 series North and South to store sales of 6 quarters
and display total (North + South)
Code:
import pandas as pd
north = pd.Series({'Q1': 2200, 'Q2': 3400, 'Q3': 2800, 'Q4': 3600, 'Q5': 3100, 'Q6': 3900})
south = pd.Series({'Q1': 1800, 'Q2': 2900, 'Q3': 3300, 'Q4': 2500, 'Q5': 3000, 'Q6': 4100})
print("North:")
print(north)
print("\nSouth:")
print(south)
print("\nTotal Sales (North + South):")
print(north + south)
Output:
North:
Q1 2200
Q2 3400
Q3 2800
Q4 3600
Q5 3100
Q6 3900
dtype: int64
South:
Q1 1800
Q2 2900
Q3 3300
Q4 2500
Q5 3000
Q6 4100
dtype: int64
Total Sales (North + South):
Q1 4000
Q2 6300
Q3 6100
Q4 6100
Q5 6100
Q6 8000
dtype: int64
Q12. Consider 'north' series and write statements: a) first 3
elements using loc b) first 3 using head c) last 3 using tail d) first
5 elements e) alternate elements f) modify 2nd element to 4500 g)
elements > 3000
Code:
import pandas as pd
north = pd.Series({'Q1': 2200, 'Q2': 3400, 'Q3': 2800, 'Q4': 3600, 'Q5': 3100, 'Q6': 3900})
print("(a) first 3 using loc:")
print(north.loc['Q1':'Q3'])
print("\n(b) first 3 using head:")
print(north.head(3))
print("\n(c) last 3 using tail:")
print(north.tail(3))
print("\n(d) first 5 elements:")
print(north.head(5))
print("\n(e) alternate elements:")
print(north.iloc[::2])
print("\n(f) modify 2nd element (Q2) to 4500:")
north.loc['Q2'] = 4500
print(north)
print("\n(g) elements > 3000:")
print(north[north > 3000])
Output:
(a) first 3 using loc:
Q1 2200
Q2 3400
Q3 2800
dtype: int64
(b) first 3 using head:
Q1 2200
Q2 3400
Q3 2800
dtype: int64
(c) last 3 using tail:
Q4 3600
Q5 3100
Q6 3900
dtype: int64
(d) first 5 elements:
Q1 2200
Q2 3400
Q3 2800
Q4 3600
Q5 3100
dtype: int64
(e) alternate elements:
Q1 2200
Q3 2800
Q5 3100
dtype: int64
(f) modify 2nd element (Q2) to 4500:
Q1 2200
Q2 4500
Q3 2800
Q4 3600
Q5 3100
Q6 3900
dtype: int64
(g) elements > 3000:
Q2 4500
Q4 3600
Q6 3900
dtype: int64