DATAFRAME
DATAFRAME
QUESTION ANSWER
N.
1 Write a suitable Python code to create an empty import pandas as pd
dataframe df=pd.DataFrame()
print(df)
Delhi 40 32 24.1
I. df1.sum()
Bengaluru 31 25 36.2
Chennai 35 27 40.8
II. df1[‘Rainfall’].mean()
Mumbai 29 21 35.2
Kolkata 39 23 41.8
III. df1.loc[:, ‘Maxtemp’].median( )
Name Price
0. Nancy Drew 150 ii. Stock.loc['4']=['The Secret',800]
1. Hardy boys 180
2. Diary of a wimpy kid 225 iii. Stock=Stock.drop('Special_Price',axis=1)
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 Special_Price.
18. Mr. Som, a data analyst has designed the
DataFrame df that contains data about
Computer Olympiad with ‘CO1’, ‘CO2’, ‘CO3’,
‘CO4’, ‘CO5’ as indexes shown below. Answer
the following questions:
A. Output:
Scho Tot_stude Topp First_Runne i. (5,4)
ol nts er rup
CO PPS 40 32 8
1 ii. School tot_students Topper First_ Runner_up
CO JPS 30 18 12
2 CO3 GPS 20 18 2
CO GPS 20 18 2 CO4 MPS 18 10 8
3
CO MPS 18 10 8 B. Python statement:
4
CO BPS 28 20 8 print(df.loc['CO2': 'CO4', 'Topper'])
5
C. print(df.Tot_students-df.First_Runnerup)
df=pd.DataFrame(df,columns=["Name","Department",
"Salary"])
print(df)
(ii) df=df.drop(2)
OR
df.drop(2,axis=0)
iii.
Answer the following questions:
i. Predict the output of the following df["total"]=df["Qtr1"]+df["Qtr2"]+
python statement: df["Qtr3"]+ df["Qtr 4"]
a. print(df.size)
b. print(df[1:3])
ii. Delete the last row from the
DataFrame.
iii. Write Python statement to add a
new column Total_Sales which is the
addition of all the 4 quarter sales.
I. print(df.head(2))
II. print(df['Title'])
III. df = df.drop(‘Rating’, axis=1)
IV. print(df.loc[2:4,'Title'])
Write Python statements for the DataFrame df V. df.rename(columns={'Title':'Name'},
to: inplace=True)
I. Print the first two rows of the DataFrame
df.
II. Display titles of all the movies.
III. Remove the column rating.
IV. Display the data of the 'Title' column
from indexes 2 to 4 (both included)
V. Rename the column name 'Title' to
'Name'
29 What are the purpose of 1. It displays the names of columns of the Dataframe.
following statements- 2. It will display all columns except the last 5 columns.
1. df.columns 3. It displays all columns with row index 2 to 7.
2. df.iloc[ : , :-5] 4. It will display entire dataframe with all rows and
columns.
3. df[2:8]
5. It will display all rows except the last 4 four rows.
4. df[ :]
5. df.iloc[ : -4 , : ]
xiic={'sub':['eng','mat','ip','acc','bst','eco'],'id':
['302','041','065','055','056','057']}
df1=pd.DataFrame(xiia)
print(df1)
df2=pd.DataFrame(xiic)
print(df2)
print(df1.merge(df2,on='id'))
print(df1.merge(df2,on='id',how='outer'))