Worksheet On Pandas Dataframe
Worksheet On Pandas Dataframe
QN Questions M
1 Write a Python code to create a DataFrame stock with appropriate column headings 2
from the list given below:
[[101,'Gurman',98], [102,'Rajveer',95], [103,'Samar' ,96], [104,'Yuvraj',88]]
7 What will be the output of following code and Answer the following: 2
i. List the index of the DataFrame df
ii. List the column names of DataFrame df.
import pandas as pd
Year1={'Q1':5000,'Q2':8000,'Q3':12000,'Q4': 18000}
Year2={'A' :13000,'B':14000,'C':12000} totSales={1:Year1,2:Year2}
df=pd.DataFrame(totSales)
print(df)
8 Consider the given DataFrame ‘Stock’: 4
Name Price
0 Nancy Drew 150
1 Hardy boys 180
2 Diary of a wimpy kid 225
3 Harry Potter 500
Write suitable Python statements for the following:
i. Add a column called Special_Price with the following data:
[135,150,200,440].
ii. Add a new book named ‘The Secret' having price 800.
iii. Remove the column Price.
iv.Remove first and third rows from DataFrame
9 Consider the given DataFrame df: 4
Q1 Q2 Q3 Q4
0 5000 8000 12000 18000
1 10 20 30 40
Write suitable Python statements for the following:
i. Add a column called Q5 with the following data: [2000,44].
ii. Add a new record with appropriate values.
iii. Remove the column Q2, Q4. iv.Remove first and second rows from
DataFrame.
17 What will be the output of following code and Answer the following: 2
i. List the index of the DataFrame df
ii. List the column names of DataFrame df.
import pandas as pd
x={'Qtr1':5000,'Qtr2':8000,'Qtr3':12000,'Qtr4': 18000} y={'p'
:13000,'q':14000,'r':12000} totSales={1:x,2:y}
df=pd.DataFrame(totSales)
print(df)