Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
13 views7 pages

Class12 Python File Work Solutions Part3 Q13-Q18

The document provides various examples of creating dataframes in Python using the pandas library, demonstrating multiple methods such as dictionaries, lists, and arrays. It includes operations performed on these dataframes, such as adding and deleting rows and columns, filtering data, and modifying indices. The examples cover dataframes for cities, marks, employee information, and fruits, showcasing practical applications of data manipulation in pandas.

Uploaded by

sharmashweta7819
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views7 pages

Class12 Python File Work Solutions Part3 Q13-Q18

The document provides various examples of creating dataframes in Python using the pandas library, demonstrating multiple methods such as dictionaries, lists, and arrays. It includes operations performed on these dataframes, such as adding and deleting rows and columns, filtering data, and modifying indices. The examples cover dataframes for cities, marks, employee information, and fruits, showcasing practical applications of data manipulation in pandas.

Uploaded by

sharmashweta7819
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Q13.

Create dataframe df1 (City, State) using multiple methods


Code:
import pandas as pd

# 1) dictionary of list
df1_list = pd.DataFrame({'City': ['mumbai','dehradun','benguluru','hyderabad'],
'state': ['maharastra','uttrakhand','karnatka','telengana']})
print("Dictionary of List:")
print(df1_list)

# 2) dictionary of series
df1_series = pd.DataFrame({'City': pd.Series(['mumbai','dehradun','benguluru','hyderabad']),
'state': pd.Series(['maharastra','uttrakhand','karnatka','telengana']
print("\nDictionary of Series:")
print(df1_series)

# 3) dictionary of array
import numpy as np
df1_array = pd.DataFrame({'City': np.array(['mumbai','dehradun','benguluru','hyderabad']),
'state': np.array(['maharastra','uttrakhand','karnatka','telengana'])}
print("\nDictionary of Array:")
print(df1_array)

# 4) dictionary of dictionary
df1_dict = pd.DataFrame({
0: {'City':'mumbai','state':'maharastra'},
1: {'City':'dehradun','state':'uttrakhand'},
2: {'City':'benguluru','state':'karnatka'},
3: {'City':'hyderabad','state':'telengana'}
}).T
df1_dict.index = [0,1,2,3]
print("\nDictionary of Dictionary:")
print(df1_dict)
Output:
Dictionary of List:
City state
0 mumbai maharastra
1 dehradun uttrakhand
2 benguluru karnatka
3 hyderabad telengana

Dictionary of Series:
City state
0 mumbai maharastra
1 dehradun uttrakhand
2 benguluru karnatka
3 hyderabad telengana

Dictionary of Array:
City state
0 mumbai maharastra
1 dehradun uttrakhand
2 benguluru karnatka
3 hyderabad telengana

Dictionary of Dictionary:
City state
0 mumbai maharastra
1 dehradun uttrakhand
2 benguluru karnatka
3 hyderabad telengana

Q14. Create dataframe of marks (Arnab, Rashmi, Samridhi, Ria) by


multiple methods
Code:
import pandas as pd
import numpy as np

data = [['Math',90,92,89,81],
['Sci',56,48,91,71],
['Hindi',97,96,35,99]]
columns = ['Subject','Arnab','Rashmi','Samridhi','Ria']

# 1) nested list
df_list = pd.DataFrame(data, columns=columns).set_index('Subject')
print("Nested List:")
print(df_list)

# 2) list of series
s1 = pd.Series([90,56,97], index=['Math','Sci','Hindi'], name='Arnab')
s2 = pd.Series([92,48,96], index=['Math','Sci','Hindi'], name='Rashmi')
s3 = pd.Series([89,91,35], index=['Math','Sci','Hindi'], name='Samridhi')
s4 = pd.Series([81,71,99], index=['Math','Sci','Hindi'], name='Ria')
df_series = pd.concat([s1,s2,s3,s4], axis=1)
print("\nList of Series:")
print(df_series)

# 3) list of dictionary
lod = [
{'Subject':'Math','Arnab':90,'Rashmi':92,'Samridhi':89,'Ria':81},
{'Subject':'Sci','Arnab':56,'Rashmi':48,'Samridhi':91,'Ria':71},
{'Subject':'Hindi','Arnab':97,'Rashmi':96,'Samridhi':35,'Ria':99}
]
df_lod = pd.DataFrame(lod).set_index('Subject')
print("\nList of Dictionary:")
print(df_lod)

# 4) list of array
arr = np.array([[90,92,89,81],
[56,48,91,71],
[97,96,35,99]])
df_array = pd.DataFrame(arr, index=['Math','Sci','Hindi'], columns=['Arnab','Rashmi','Samridhi',
print("\nList of Array:")
print(df_array)

# 5) dictionary of list
dol = {'Arnab':[90,56,97], 'Rashmi':[92,48,96], 'Samridhi':[89,91,35], 'Ria':[81,71,99]}
df_dol = pd.DataFrame(dol, index=['Math','Sci','Hindi'])
print("\nDictionary of List:")
print(df_dol)
Output:
Nested List:
Arnab Rashmi Samridhi Ria
Subject
Math 90 92 89 81
Sci 56 48 91 71
Hindi 97 96 35 99

List of Series:
Arnab Rashmi Samridhi Ria
Math 90 92 89 81
Sci 56 48 91 71
Hindi 97 96 35 99

List of Dictionary:
Arnab Rashmi Samridhi Ria
Subject
Math 90 92 89 81
Sci 56 48 91 71
Hindi 97 96 35 99

List of Array:
Arnab Rashmi Samridhi Ria
Math 90 92 89 81
Sci 56 48 91 71
Hindi 97 96 35 99

Dictionary of List:
Arnab Rashmi Samridhi Ria
Math 90 92 89 81
Sci 56 48 91 71
Hindi 97 96 35 99

Q15. q 27 from board paper 2024


Note:
The exact question text is not provided in your file. Please share the question or a photo/screenshot
from the board paper so I can include the full code and output here.

Q16. Q9 and Q11(c to h) from NCERT pg 61


Note:
The page’s question statements are required to produce accurate solutions. Please send the text or
a clear photo of NCERT page 61 so I can add complete solutions.

Q17. Create dataframe 'emp' using dictionary of series and


perform operations (a–h)
Code:
import pandas as pd

emp = pd.DataFrame({
'Name': pd.Series(['atul','raj','darpan','anmol','piyush'], index=['E101','E102','E103','E10
'designation': pd.Series(['manager','clerk','analyst','clerk','manager'], index=['E101','E10
'salary': pd.Series([56000,25000,35000,2800,58000], index=['E101','E102','E103','E104','E105
'bonus': pd.Series([15000,7000,9000,1300,12000], index=['E101','E102','E103','E104','E105'])
})
print("Original emp:")
print(emp)

# a) display name, designation and salary


print("\n(a) Columns name, designation, salary:")
print(emp[['Name','designation','salary']])

# b) display info from empid E102 to E104


print("\n(b) Rows E102 to E104:")
print(emp.loc['E102':'E104'])

# c) add one more employee


emp.loc['E106'] = ['rohan','administrator',65000,23000]
print("\n(c) After adding E106:")
print(emp)

# d) delete data for employee E102


emp = emp.drop('E102')
print("\n(d) After deleting E102:")
print(emp)

# e) add column 'joining year'


emp['joining year'] = [2002,2006,1994,1998,2020][:len(emp)]
print("\n(e) After adding 'joining year':")
print(emp)

# f) employees with bonus > 15000


print("\n(f) Bonus > 15000:")
print(emp[emp['bonus'] > 15000])

# g) delete column salary


emp = emp.drop(columns=['salary'])
print("\n(g) After deleting 'salary' column:")
print(emp)

# h) rename column bonus to stipend


emp = emp.rename(columns={'bonus':'stipend'})
print("\n(h) After renaming 'bonus' to 'stipend':")
print(emp)
Output:
Original emp:
Name designation salary bonus
E101 atul manager 56000 15000
E102 raj clerk 25000 7000
E103 darpan analyst 35000 9000
E104 anmol clerk 2800 1300
E105 piyush manager 58000 12000

(a) Columns name, designation, salary:


Name designation salary
E101 atul manager 56000
E102 raj clerk 25000
E103 darpan analyst 35000
E104 anmol clerk 2800
E105 piyush manager 58000

(b) Rows E102 to E104:


Name designation salary bonus
E102 raj clerk 25000 7000
E103 darpan analyst 35000 9000
E104 anmol clerk 2800 1300

(c) After adding E106:


Name designation salary bonus
E101 atul manager 56000 15000
E102 raj clerk 25000 7000
E103 darpan analyst 35000 9000
E104 anmol clerk 2800 1300
E105 piyush manager 58000 12000
E106 rohan administrator 65000 23000

(d) After deleting E102:


Name designation salary bonus
E101 atul manager 56000 15000
E103 darpan analyst 35000 9000
E104 anmol clerk 2800 1300
E105 piyush manager 58000 12000
E106 rohan administrator 65000 23000

(e) After adding 'joining year':


Name designation salary bonus joining year
E101 atul manager 56000 15000 2002
E103 darpan analyst 35000 9000 2006
E104 anmol clerk 2800 1300 1994
E105 piyush manager 58000 12000 1998
E106 rohan administrator 65000 23000 2020

(f) Bonus > 15000:


Name designation salary bonus joining year
E106 rohan administrator 65000 23000 2020

(g) After deleting 'salary' column:


Name designation bonus joining year
E101 atul manager 15000 2002
E103 darpan analyst 9000 2006
E104 anmol clerk 1300 1994
E105 piyush manager 12000 1998
E106 rohan administrator 23000 2020

(h) After renaming 'bonus' to 'stipend':


Name designation stipend joining year
E101 atul manager 15000 2002
E103 darpan analyst 9000 2006
E104 anmol clerk 1300 1994
E105 piyush manager 12000 1998
E106 rohan administrator 23000 2020

Q18. Create dataframe for fruits and perform operations (a–h)


Code:
import pandas as pd

data = [
{'Colour':'red','name':'apple','qty':10},
{'Colour':'blue','name':'berry','qty':15},
{'Colour':'green','name':'guava','qty':20}
]
df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)

# b) display colour of all fruits


print("\n(b) Colour of all fruits:")
print(df['Colour'])

# c) display alternate rows


print("\n(c) Alternate rows:")
print(df.iloc[::2])

# d) rows where qty < 15


print("\n(d) qty < 15:")
print(df[df['qty'] < 15])

# e) change row index to fruit1, fruit2, ...


df.index = [f'fruit{i}' for i in range(1, len(df)+1)]
print("\n(e) Change row index:")
print(df)

# f) delete 2nd record


df2 = df.drop('fruit2')
print("\n(f) After deleting 2nd record:")
print(df2)

# g) add new column cost


df2['cost'] = [120, 220] # costs for remaining rows
print("\n(g) After adding cost:")
print(df2)

# h) add new row brown chikoo 40, 650


df2.loc['fruit3'] = {'Colour':'brown','name':'chikoo','qty':40,'cost':650}
print("\n(h) After adding new row:")
print(df2)
Output:
Original DataFrame:
Colour name qty
0 red apple 10
1 blue berry 15
2 green guava 20

(b) Colour of all fruits:


0 red
1 blue
2 green
Name: Colour, dtype: object

(c) Alternate rows:


Colour name qty
0 red apple 10
2 green guava 20

(d) qty < 15:


Colour name qty
0 red apple 10

(e) Change row index:


Colour name qty
fruit1 red apple 10
fruit2 blue berry 15
fruit3 green guava 20

(f) After deleting 2nd record:


Colour name qty
fruit1 red apple 10
fruit3 green guava 20

(g) After adding cost:


Colour name qty cost
fruit1 red apple 10 120
fruit3 green guava 20 220

(h) After adding new row:


Colour name qty cost
fruit1 red apple 10 120
fruit3 green guava 20 220
fruit3 brown chikoo 40 650

You might also like