0% found this document useful (0 votes)
129 views6 pages

DATAFRAME

Uploaded by

Rajitha
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)
129 views6 pages

DATAFRAME

Uploaded by

Rajitha
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/ 6

DATAFRAME CREATION

1) Write python code to create the DataFrame employee using dictionary:

2) Write a python code to create a DataFrame with appropriate column headings

from the list given below:

[[201,’Gurmeet’,95],[202,’Praveen’,89],[203,’Suman’,97],[204.’Yogesh’,91]]

3) Write a Python code to create a DataFrame ‘stud’ using series with specified column
headings, index and data:

4) Write python code to create a dataframe by using following data:


[[201,’Manoj’,4500],[202,’Dhara’,3200],[203,’Mohini’,2300]]
i. Dataframe name should be cust
ii. Use column headings as: cust_id,cust_name and amount

5) Write a Python code to create a DataFrame ‘Toppers’ using dictionary of series with
specified column headings, index and data:

6) Write a Python code to create a DataFrame ‘stud’ using list of dictionaries with
specified column headings, index and data:

7) Write a Python program to create an empty dataframe.

8) Write a Python code to create a DataFrame ‘Df’ using dictionary of dictionaries for
the following data.
9) Write a Python code to create a DataFrame with appropriate column headings from
the list given
below:
[[1001,'IND-AUS',’2022-10-17’], [1002,'IND-PAK',’2022-10-23’], [1003,'IND-SA' ,
‘2022-10-30],
[1004,'IND-NZ',’2022-11-18’]]

10) Write a Python code to create the below dataframe ‘Items’ by passing 3 series

DATAFRAME OPERATIONS

1) Consider the following DataFrame df and answer the questions.

Write suitable Python statements for the following:


i. Change the population in Kolkata as 500000.
ii. Rename the index population as “pop”.
iii. Display the number of hospitals in Delhi.
iv. Rename the column ‘Bombay’ as ‘Mumbai’.

2) Consider the given DataFrame ‘Fees’:

Write suitable Python statements for the following:


i. Add a column called ‘Section’ with the following data:
[‘A’,’B’,’C’,’D’].
ii. Add a new class name named ‘IX' having price 1800.
iii. Remove the column ‘Section’.
v. Set the column ‘Cname’ as the index of the dataframe.

3) Consider the given DataFrame ‘df’:

Name Price
CHESS 150
CARROM BOARD 900
LUDO 100
FOOTBALL 700
Write suitable Python statements for the following:
i. Add a column called DISCOUNT with the following data: [15,90,10,70].
ii. Add a row with the values BADMINTON 200 20
iii. Remove the column DISCOUNT.

4) Consider the following dataframe ‘temperature’:

Write suitable python code to:


a) Add new column min with these data: [25,22,19,28]
b) Add new city Mahesana with maximum value 30.
c) Rename the column Max as Maximum

5) Consider the given DataFrame ‘df’:

Name Price Discount


0 CHESS 150 15
1 CARROM BOARD 900 90
2 LUDO 100 10
3 FOOTBALL 700 70

Write suitable python code to:

i. Set the index of the column as G1, G2, G3 and G4.


ii. Rename the column Discount as ‘Discount Percentage’.
iii. Add a new column Total as the sum of Price and Discount.

1) Carefully observe the following code:

import pandas as pd
data = [{'a': 10, 'b': 20},{'a': 6, 'b': 32, 'c': 22}]
df1 = pd.DataFrame(data)
print(df1)

Answer the following:


i. List the index of the DataFrame df1
ii. List the column names of DataFrame df1.

2) Consider the code given below and answer the following question:

Ld=[{'a':10,'b':20},{'a':5,'b':10,'c':20}]
df=pd.DataFrame(Ld)
print(df)

i) Write the missing import statement in the above code.


ii) How many columns will be there in the dataframe.
3) Observe the following code and write statements for the below given questions:

import pandas as pd
player1={'IG1':34,'IG2':0,'IG3':23}
player2={'IG1':21,'IG2':10,'IG3':39}
pl={'p1':player1,'p2':player2}
df=pd.DataFrame(pl)

a) Display data of player scored more than 30 runs.


b) Display all rows of first inning of all players.

4) Carefully observe the following code:

import pandas as pd
L=[['S101','Anushree',65],['S102','Anubha',56],['S104','Vishnu',67],['S105','Kritika',45]]
df=pd.DataFrame (L, columns=['ID','Name','Marks'])
print(df)

i. What is the shape of the data frame df?


ii. Name the index and column names of dataframe df?

5) Carefully observe the following code:

import pandas as pd
D1={'S1': 'India', 'S2': 'Russia', 'S3': 'World'}
D2={'School': 'EOIS', 'Place': 'Moscow'}
data={1:D1,2:D2}
df=pd.DataFrame(data)
print(df)

Answer the following


i. List the index of the DataFrame df.
ii. List the column names of DataFrame df.

Dataframe accessing & slicing

1) Given the dataframe items and perform the following operations:

Write the python statement to:


i) Access the price of keyboard
ii) Get the total number of mouse available.
iii) Return the names of the items which has price more than 1000.
iv) Get the price and quantity of watch.
v) To return the quantity of mouse.

Write the output:


i) print(items.iloc[2,2])
ii) print(items.iloc[[1,2],2])
iii) print(items[1:3])
iv) print(items['c':'d'])
v) print(items.iloc[0:1, 1 : 2])
vi) print(items.iloc[0:1, 1])

2) Give dataframe df and perform the following:

a) Write the output for the following

i) print(df[1:3])
ii) print(df.loc[1,'Quantity'])

b) Write the python statement to get the price of the watch using loc function.

3) Given a dataframe df

a) Write the output for the following:

i) print(df.loc[1,'Quantity'])
ii) print(df[1:3])
iii) print(df.iloc[1:3,2])
iv) print(df.loc[1:2,'Price'])
v) print(df.iloc[1:2,2:4])

DATAFRAME ATTRIBUTES & METHODS

1) Given a dataframe df:

Write Python statements for the following:

i) To return the total number of values (elements) in the dataframe.


ii) To display the rows labels of dataframe.
a) df.rows
b) df.index
c) df.labes
d) Both a and b

iii) To display the column names of the dataframe.


iv) To display the datatypes of columns name and quantity.
v) To display the numpy array having all the values in the DataFrame,
without the axes labels.
vi) To display a tuple representing the dimensionality of the DataFrame.
vii) To replace the row indices and column labels of the dataframe with each
other’s position.
viii) To display the first 3 rows in the dataframe.
ix) To display the last 5 rows in the dataframe.
x) To returns the value True if dataframe is empty and False otherwise.
xi) To check if dataframe has NaN value.

2) Given a dataframe df:

Write the output for the following statement:

i) print(df.size)
ii) print(df.count())
iii) print(df.isnull())
iv) print(df['Price'].isnull())
v) print(df['Quantity'].count())
vi) print(df.T)
vii) print(df.ndim)
viii) print(df['Quantity'].values)
ix) print(df.empty)
x) print(df.shape)

You might also like