SlideShare a Scribd company logo
1 of 55
PANDAS
DATAFRAME
Data Handling using Pandas – 1
Data Frames:
creation - from dictionary of Series, list of dictionaries, Text/CSV
files; display; iteration;
Operations on rows and columns: add, select, delete, rename;
Head and Tail functions;
Indexing using Labels, Boolean Indexing;
Importing/Exporting Data between CSV files and Data Frames.
1
Sangita Panchal
Pandas DataFrame
 A pandas DataFrame is a two-dimensional array.
 Data is aligned in rows and columns.
The general format:
pandas. DataFrame (data, index, columns, dtype)
data: It is like ndarray, series, list, dict, constants or another
DataFrame.
index: It is for row label, by default 0..n-1 (np.arange(n))
columns: It is for column label, by default 0…n-1 (np.arange(n))
dtype: data type of each column
2
Sangita Panchal
Key Features:
• Heterogeneous data
• Size Mutable
• Data Mutable
Creating DataFrame using list 3
Sangita Panchal
import pandas as pd
D = pd.DataFrame([[10,20],[30,40]])
print(D)
0 1
0 10 20
1 30 40
Default columns
Default rows
Creating DataFrame with row index and column label 4
Sangita Panchal
import pandas as pd
data = [[10,20],[30,40]]
D = pd.DataFrame(data,columns = ['col1','col1'],index = ['row1','row2'])
print(D)
col1 col1
row1 10 20
row2 30 40
Column Label
Row Label
Creating DataFrame using dictionary 5
Sangita Panchal
import pandas as pd
data = {'Name':['Anu','Sia'],'Marks':[19,25]}
D = pd.DataFrame(data,index = [1,2])
The column names
are the keys in a
dictionary.
Rows using
index
Creating DataFrame from dictionary of Series 6
Sangita Panchal
import pandas as pd
d = {'one' : pd.Series([10, 20, 30, 40], index =['a', 'b', 'c', 'd']),
'two' : pd.Series([10, 20, 30, 40], index =['a', 'b', 'c', 'd'])}
df = pd.DataFrame(d)
print(df )
Creating DataFrame from list of dictionary 7
Sangita Panchal
import pandas as pd
data = [{'b': 2, 'c':3}, {'a': 10, 'b': 20, 'c': 30}]
df = pd.DataFrame(data, index =['first', 'second'])
df = pd.DataFrame(d)
print(df )
Writing DataFrame to csv file 8
Sangita Panchal
import pandas as pd
data = {'Name':['Anu','Sia'],'Marks':[19,25]}
D = pd.DataFrame(data,index = [1,2])
print(D)
D.to_csv("E:stu.csv")
Writing DataFrame
data to Excel file
Creating DataFrame using text/csv file 9
Sangita Panchal
import pandas as pd
>>> data = pd.read_csv("C:/Users/admin/Desktop/stock.csv")
>>> print(data)
No Name Qty
0 1 Pencil 12
1 2 Pen 20
2 3 Eraser 17
3 4 Sharpner 10
Create .csv file using
 Notepad
 Excel
Read data in Excel
file to DataFrame
Iteration in DataFrame 10
Sangita Panchal
import pandas as pd
Data = [[1006,1008],['A1','B1'],[23000,19000]]
D = pd.DataFrame(Data)
print(D)
print(D.shape, D.size)
for row in range(D.shape[0]):
print("Row ",row,"Columns",end = " ")
for col in range(D.shape[1]):
print(D[col][row], end = " ")
print()
0 1
0 1006 1008
1 A1 B1
2 23000 19000
(3, 2) 6
Row 0 Columns 1006 1008
Row 1 Columns A1 B1
Row 2 Columns 23000 1900
Iteration in DataFrame using iterrows() 11
Sangita Panchal
import pandas as pd
Data = {'Name': ['Amy','Anu','Jia’],
'Total':[45,32,76]}
D = pd.DataFrame(Data)
print(D)
for i in D.iterrows():
print(i)
Name Total
0 Amy 45
1 Anu 32
2 Jia 76
(0, Name Amy
Total 45
Name: 0, dtype: object)
(1, Name Anu
Total 32
Name: 1, dtype: object)
(2, Name Jia
Total 76
Name: 2, dtype: object)
iterrows() - used for iterating over
the rows as (index, series) pairs.
Add column, Insert column 12
Sangita Panchal
import pandas as pd
data = {'Name':['Anu','Sia'],'M1':[19,25]}
D = pd.DataFrame(data,index = [1,2])
print(D)
D['M2'] = [13,34]
D['Total'] = D['M1']+D['M2']
print("Added columns ", D, sep = "n")
print(“Inserted columns”)
D.insert(1,"Remarks",[‘A',’B'])
print(D)
Name M1
1 Anu 19
2 Sia 25
Added columns
Name M1 M2 Total
1 Anu 19 13 32
2 Sia 25 34 59
Inserted column
Name Remarks M1 M2 Total
1 Anu A 19 13 32
2 Sia B 25 34 59
Add Row at the end 13
Sangita Panchal
import pandas as pd
data = {'Name':['Anu','Sia'],'M1':[19,25], 'M2': [13,34],
'Total':[32, 59]}
D = pd.DataFrame(data,index = [1,2])
print(D)
D.loc[3] = ['Jia',23,24,47]
print("Added Row using loc",D, sep = "n")
Name M1 M2 Total
1 Anu 19 13 32
2 Sia 25 34 59
Added Row
Name M1 M2 Total
1 Anu 19 13 32
2 Sia 25 34 59
3 Jia 23 24 47
D = D.append({'Name':'Somy','M1':21,'M2':19,'Total':40},ignore_index = True)
print("Added Row using append",D, sep = "n")
Added Row using append
Name M1 M2 Total
0 Anu 19 13 32
1 Sia 25 34 59
2 Jia 23 24 47
3 Somy 21 19 40
Select Column 14
Sangita Panchal
print(D['Total'])
print(D[['M1', 'M2']])
print(D.Name)
Name M1 M2 Total
0 Anu 19 13 32
1 Sia 25 34 59
2 Jia 23 24 47
0 32
1 59
2 47
Name: Total, dtype: int64
M1 M2
0 19 13
1 25 34
2 23 24
0 Anu
1 Sia
2 Jia
Name: Name, dtype: object
You can select a column using dot notation or
using square brackets.
Delete column, row 15
Sangita Panchal
D.drop(columns = ['Name’], inplace = True)
print(D)
D.drop(columns = [‘M1’,’M2’], inplace = True)
print(D)
D.drop(index = [0,1],inplace = True)
print(D)
Name M1 M2 Total
0 Anu 19 13 32
1 Sia 25 34 59
2 Jia 23 24 47
M1 M2 Total
0 19 13 32
1 25 34 59
2 23 24 47
Total
0 32
1 59
2 47
Total
2 47
To delete single column - del, pop(), drop()
To delete multiple columns - drop()
To delete single / multiple rows - drop()
Rename column 16
Sangita Panchal
D.rename(columns = {'Name':'StuName'},inplace = True)
print(D)
D.rename(index = {0:'zero',1:'one’},columns = {"Total":"T"},
inplace = True)
print(D)
Name M1 M2 Total
0 Anu 19 13 32
1 Sia 25 34 59
2 Jia 23 24 47
StuName M1 M2 Total
0 Anu 19 13 32
1 Sia 25 34 59
2 Jia 23 24 47
StuName M1 M2 T
zero Anu 19 13 32
one Sia 25 34 59
2 Jia 23 24 47
Change column name
Change row index
Rename column label/row index 16a
Sangita Panchal
print(D.columns)
print(D.index)
D.columns = [‘a’,’b’,’c’,’d’]
D.index = [1,2,3]
StuName M1 M2 T
zero Anu 19 13 32
one Sia 25 34 59
2 Jia 23 24 47
Index(['StuName', 'M1', 'M2', 'T'], dtype='object')
Index(['zero', 'one', 2], dtype='object’)
a b c d
zero Anu 19 13 32
one Sia 25 34 59
2 Jia 23 24 47
a b c d
1 Anu 19 13 32
2 Sia 25 34 59
3 Jia 23 24 47
Head and Tail operation 17
Sangita Panchal
print("Top 2 rows",D.head(2),sep = "n")
print("Bottom 2 rows",D.tail(2), sep = "n")
Name M1 M2 Total
0 Anu 19 13 32
1 Sia 25 34 59
2 Jia 23 24 47 Top 2 rows
Name M1 M2 Total
0 Anu 19 13 32
1 Sia 25 34 59
Bottom 2 rows
Name M1 M2 Total
1 Sia 25 34 59
2 Jia 23 24 47
Note: The default of head() and tail() is 5.
print(D.head() )displays top 5 rows.
print(D.tail()) displays bottom 5 rows.
Indexing using Label /Position 18
Sangita Panchal
There are 3 ways of indexing:
1. DF[“col1”]
DF[[“col1”,”col2”]]
2. DF.loc[“col1”]
DF.loc[[“col1”,”col2”]]
DF.loc[[“row1”,”row2”],[“col1”,”col2”]]
DF.loc[ : :, : :]
3. DF.iloc[3]
DF.iloc[[1,3,4]]
DF.iloc[[3,4],[1,2]]
DF.iloc[ : : , : :]
For column only
For position
(Default index ]
Used for Labels
Indexing using Label / Position 19
Sangita Panchal
print("Select Name column ", D['Name'],sep = "n")
print("Select Row and column ",D.iloc[::2,1:3],sep = "n")
print("Select second row",D.iloc[1,:],sep = "n")
Name M1 M2 Total
0 Anu 19 13 32
1 Sia 25 34 59
2 Jia 23 24 47
Select Name column
0 Anu
1 Sia
2 Jia
Name: Name, dtype: object
Select Row and column
M1 M2
0 19 13
2 23 24
Select second row
Name Sia
M1 25
M2 34
Total 59
Name: 1, dtype: object47
Indexing using Label / Position 20
Sangita Panchal
print(D["Total"])
print(D.iloc[[1,2],[0,1]])
print(D.loc[[1,2],["Name","M1"]])
print(D.iloc[0:3:2,::2])
print(D.loc[0:3:2,"Name":"Total":2])
Indexing using Label / Position 21
Sangita Panchal
print(D.Total > 50)
print((D.M1 > 20) & (D.M2 > 20))
print((D.M1 > 20) | (D.M2 > 20))
print(~(D.Total> 40))
~ NOT
& AND
| OR
Indexing using Label / Position 22
Sangita Panchal
print(D[(D.M1 > 20) & (D.M2 > 20)])
print(D[(D.M1 > 20) | (D.M2 > 20)])
print(D[~(D.Total> 40)])
Append two DataFrame and sort 23
Sangita Panchal
import pandas as pd
data = {'Name':['A','B'],'Total':[89,78]}
D = pd.DataFrame(data)
print(D)
data1 = {'Name':['C','D'],'Total':[78,91]}
D1 = pd.DataFrame(data1)
print(D1)
D2 = D.append(D1)
print(D2)
print(D2.sort_values('Total', ascending = False))
max, min, sum, mean, count 24
Sangita Panchal
print(D.max())
print(D.count(axis = 1))
print(D['Total'].min())
print(D[['M1','M2']].sum())
print(D[['M1','M2','Total']].mean())
M1 42.0
M2 71.0
dtype: float64
M1 21.000000
M2 23.666667
Total 46.000000
dtype: float64
Name M1 M2 Total
0 Anu 19.0 13 32
1 Sia NaN 34 59
2 Sid 23.0 24 47
Name Sid
M1 23
M2 34
Total 59
dtype: object
0 4
1 3
2 4
dtype: int64
32
By default, these functions are applied to rows (axis =0).
For columns, axis = 1 is mandatory.
Note: D.max() cann be written as D.max(0)
D.count(axis = 1) can be written as D.count(1)
CBSE QUESTIONS 25
Sangita Panchal
1. In a DataFrame, Axis= 1 represents the_____________ elements.
2. In Pandas the function used to check for null values in a DataFrame is ________
3. Consider the following DataFrame df and answer any four questions from (i)-(v)
rollno name UT1 UT2 UT3 UT4
1 Prerna Singh 24 24 20 22
2 Manish Arora 18 17 19 22
3 Tanish Goel 20 22 18 24
4 Falguni Jain 22 20 24 20
5 Kanika Bhatnagar 15 20 18 22
6 Ramandeep Kaur 20 15 22 24
CBSE QUESTIONS 26
Sangita Panchal
1. In a DataFrame, Axis= 1 represents the_____________ elements.
2. In Pandas the function used to check for null values in a DataFrame is ________
3. Consider the following DataFrame df and answer any four questions from (i)-(v)
rollno name UT1 UT2 UT3 UT4
1 Prerna Singh 24 24 20 22
2 Manish Arora 18 17 19 22
3 Tanish Goel 20 22 18 24
4 Falguni Jain 22 20 24 20
5 Kanika Bhatnagar 15 20 18 22
6 Ramandeep Kaur 20 15 22 24
column
isnull()
CBSE QUESTIONS 27
Sangita Panchal
(I) Write down the command that will give the following output:
a. print(df.max)
b. print(df.max())
c. print(df.max(axis=1))
d. print(df.max, axis=1)
CBSE QUESTIONS 28
Sangita Panchal
(I) Write down the command that will give the following output:
a. print(df.max)
b. print(df.max())
c. print(df.max(axis=1))
d. print(df.max, axis=1)
print(df.max())
CBSE QUESTIONS 29
Sangita Panchal
The teacher needs to know the marks scored by the student with roll number 4. Help her
to identify the correct set of statement/s from the given options :
a. df1=df[df[‘rollno’]==4] ; print(df1)
b. df1=df[rollno==4] ; print(df1)
c. df1=df[df.rollno=4] ; print(df1)
d. df1=df[df.rollno==4] ; print(df1)
v. Ms. Sharma, the class teacher wants to add a new column, the scores of Grade with the
values, ‘ A’, ‘B’, ‘A’, ‘A’, ‘B’, ‘A’ ,to the DataFrame. Help her choose the command to do so:
a. df.column=[’A’,’B’,’A’,’A’,’B’,’A’]
b. df [‘Grade’]=[’A’,’B’,’A’,’A’,’B’,’A’]
c. df.loc[‘Grade’]= [’A’,’B’,’A’,’A’,’B’,’A’]
d. Both (b) and (c) are correct
CBSE QUESTIONS 30
Sangita Panchal
The teacher needs to know the marks scored by the student with roll number 4. Help her
to identify the correct set of statement/s from the given options :
a. df1=df[df[‘rollno’]==4] ; print(df1)
b. df1=df[rollno==4] ; print(df1)
c. df1=df[df.rollno=4] ; print(df1)
d. df1=df[df.rollno==4] ; print(df1)
v. Ms. Sharma, the class teacher wants to add a new column, the scores of Grade with the
values, ‘ A’, ‘B’, ‘A’, ‘A’, ‘B’, ‘A’ ,to the DataFrame. Help her choose the command to do so:
a. df.column=[’A’,’B’,’A’,’A’,’B’,’A’]
b. df [‘Grade’]=[’A’,’B’,’A’,’A’,’B’,’A’]
c. df.loc[‘Grade’]= [’A’,’B’,’A’,’A’,’B’,’A’]
d. Both (b) and (c) are correct
a and d
b
CBSE QUESTIONS 31
Sangita Panchal
(iii) Which of the following statement/s will give the exact number of values in each column of the
dataframe?
i. print(df.count())
ii. print(df.count(0))
iii. print(df.count)
iv. print(df.count(axis=’index’))
Choose the correct option:
a. both (i) and (ii)
b. only (ii)
c. (i), (ii) and (iii)
d. (i), (ii) and (iv)
iv. Which of the following command will display the column labels of the DataFrame?
a. print(df.columns())
b. print(df.column())
c. print(df.column)
d. print(df.columns)
CBSE QUESTIONS 32
Sangita Panchal
(iii) Which of the following statement/s will give the exact number of values in each column of the
dataframe?
i. print(df.count())
ii. print(df.count(0))
iii. print(df.count)
iv. print(df.count(axis=’index’))
Choose the correct option:
a. both (i) and (ii)
b. only (ii)
c. (i), (ii) and (iii)
d. (i), (ii) and (iv)
iv. Which of the following command will display the column labels of the DataFrame?
a. print(df.columns())
b. print(df.column())
c. print(df.column)
d. print(df.columns)
a
d
CBSE QUESTIONS 33
Sangita Panchal
Consider the following DataFrame, classframe
Write commands to :
i. Add a new column ‘Activity’ to the Dataframe
ii. Add a new row with values ( 5 , Mridula ,X, F , 9.8, Science)
CBSE QUESTIONS 34
Sangita Panchal
Consider the following DataFrame, classframe
Write commands to :
i. Add a new column ‘Activity’ to the Dataframe
ii. Add a new row with values ( 5 , Mridula ,X, F , 9.8, Science)
i. classframe[‘Activity’] = [‘a’,’b’,’c’,’d’]
ii. classframe.loc[‘St5’] = [5, ‘Mridula’,’X’,’F’,9.8,’Science’]
CBSE QUESTIONS 35
Sangita Panchal
Write a program in Python Pandas to create the following DataFrame batsman from a
Dictionary:
Perform the following operations on the DataFrame :
1)Add both the scores of a batsman and assign to column “Total”
2)Display the highest score in both Score1 and Score2 of the DataFrame.
3. Display the DataFrame
CBSE QUESTIONS 36
Sangita Panchal
Write a program in Python Pandas to create the following DataFrame batsman from a
Dictionary:
Perform the following operations on the DataFrame :
1)Add both the scores of a batsman and assign to column “Total”
2)Display the highest score in both Score1 and Score2 of the DataFrame.
3. Display the DataFrame
import pandas as pd]
data = {‘B_NO’: [1,2,3,4],
‘Name’:[‘Sunil Pillai’,’Gaurav Sharma’, ‘Piyush
Goel’,’Kartik Thakur’], ‘Score1’:[90,65,70,80],
’Score2’:[80,45,90,76]}
batsman = pd.DataFrame(data)
i. batsman[‘Total’] = batsman[‘Score1’] +
batsman[‘Score2’]
ii. print(batsman[[‘Score1’,’Score2’]].max())
iii. print(batsman)
CBSE QUESTIONS 37
Sangita Panchal
Consider the dataframe df, write the output of the following:
Eng Math Sci
2017 75 95 75
2018 99 99 NaN
2019 98 95 95
a. print(df[‘Eng’].min())
b. print(df[[‘Eng’,’Sci’]].count())
c. print(df.loc[2018].max())
d. print(df.loc[2018 : 2020,’Maths’:’Sci’].max())
CBSE QUESTIONS 38
Sangita Panchal
Consider the dataframe df, write the output of the following:
Eng Math Sci
2017 75 95 75
2018 99 99 NaN
2019 98 95 95
a. print(df[‘Eng’].min())
b. print(df[[‘Eng’,’Sci’]].count())
c. print(df.loc[2018].max())
d. print(df.loc[2018 : 2019,’Maths’:’Sci’].max())
a. 75
b. Eng 3
Sci 2
c. 99.0
d. Math 99.0
Sci 95.0
CBSE QUESTIONS 39
Sangita Panchal
(a) The ______________ method in Pandas can be used to delete rows or columns.
(b) The code to display the last three rows of the dataframe df is ___________.
(c) Consider a dataframe DF
Ecode Ename
1 Sarika
2 Monica
3 Mehak
Write the command to add a new column named “Salary” from the list Sal = [15000,
18000, 16000]
Ecode Salary Ename
1 15000 Sarika
2 18000 Monica
3 16000 Mehak
CBSE QUESTIONS 40
Sangita Panchal
(a) The ______________ method in Pandas can be used to delete rows or columns.
(b) The code to display the last three rows of the dataframe df is ___________.
(c) Consider a dataframe DF
Ecode Ename
1 Sarika
2 Monica
3 Mehak
Write the command to add a new column named “Salary” from the list Sal = [15000,
18000, 16000]
Ecode Salary Ename
1 15000 Sarika
2 18000 Monica
3 16000 Mehak
drop()
df.tail(3)
DF.insert(1,”salary”, Sal)
or
DF.insert(loc = 1, column = “Salary”, value = Sal)
CBSE QUESTIONS 41
Sangita Panchal
(d) Write a Python code to create a dataframe from the list given below:
[‘E001’, ‘Govind’, 35000],[‘E002’, ‘Jatin’, 43000], [‘E003’, ‘Deepak’, 38000]
Give the column heading as EmpCode, EmpName and EmpSalary. Also give the index as
1, 2 and 3.
import pandas as pd
data = [[‘E001’, ‘Govind’, 35000],[‘E002’, ‘Jatin’, 43000], [‘E003’, ‘Deepak’, 38000]]
df = pd.DataFrame(data, columns = [‘EmpCode’, ‘EmpName’, ‘EmpSalary’], index =
[1, 2, 3])
print(df)
CBSE QUESTIONS 42
Sangita Panchal
(e) Consider te following dataframe, and answer the questions given below:
import pandas as pd
df = pd.DataFrame({“Sub1”:[56, 78, 67, 98, 65], “Sub2”: [87, 92, 76, 56, 69],
“Sub3”:[65, 64, 74, 72, 81], “Sub4”:[90, 87, 56, 66, 86]})
(i) Write the code to find the mean value row wise and column wise.
(ii) Write the code to find the sum value row wise.
(i) print(df.mean(axis = 1))
print(df.mean(axis = 0))
(ii) print(df.sum(axis = 1))
CBSE QUESTIONS 43
Sangita Panchal
(f) Given a dataframe DF of marks obtained by 100 students as shown below:
Highest Lowest Average
English 95 56 67.8
Maths 100 65 59.8
Physics 97 68 69.8
Chemistry 99 54 79.8
CompSc 100 82 90.8
(i) Write command to compute sum of every column of the dataframe.
(ii) Write command to compute mean of Average column.
(iii) Write command to compute average of Highest and Lowest columns
CBSE QUESTIONS 44
Sangita Panchal
(f) Given a dataframe DF of marks obtained by 100 students as shown below:
SubjectHighest LowestAverage
English 95 56 67.8
Maths 100 65 59.8
Physics 97 68 69.8
Chemistry 99 54 79.8
CompSc 100 82 90.8
(i) Write command to compute sum of every column of the dataframe.
(ii) Write command to compute mean of Average column.
(iii) Write command to compute average of Highest and Lowest columns
DF.sum()
DF[‘Average’].mean()
DF[[‘Highest’,’Lowest’]].mean()
CBSE QUESTIONS 45
Sangita Panchal
(g) Find the output of the following code:
import pandas as pd
data = [{'a': 10, 'b': 20},{'a': 6, 'b1': 32, 'c': 22}]
df1 = pd.DataFrame(data, index=['first', 'second'], columns=['a', 'b', ‘c’])
df2 = pd.DataFrame(data, index=['first', 'second'], columns=['a', 'b1'])
print(df1)
print(df2)
CBSE QUESTIONS 46
Sangita Panchal
(g) Find the output of the following code:
import pandas as pd
data = [{'a': 10, 'b': 20},{'a': 6, 'b1': 32, 'c': 22}]
df1 = pd.DataFrame(data, index=['first', 'second'], columns=['a', 'b', ‘c’])
df2 = pd.DataFrame(data, index=['first', 'second'], columns=['a', 'b1'])
print(df1)
print(df2) a b c
first 10 20 NaN
second 6 NaN 22
a b 1
first 10 NaN
second 6 32
CBSE QUESTIONS 47
Sangita Panchal
(h) Write the code in pandas to create the following dataframes:
df1 df2
a. Write a command to add dataframe DF1 and DF2.
b. Write a command to subtract datafram DF2 from DF1.
c. Write a command to rename DF1 column Marks1 to M1.
d. Write a command to rename DF1 row index 0 to zero, 1 to one.
CBSE QUESTIONS 48
Sangita Panchal
(h) Write the code in pandas to create the following dataframes:
df1 df2
a. Write a command to add dataframe DF1 and DF2.
b. Write a command to subtract datafram DF2 from DF1.
c. Write a command to rename DF1 column Marks1 to M1.
d. Write a command to rename DF1 row index 0 to zeor, 1 to one.
import pandas as pd
DF1 = pd.DataFrame({'Mark1':[49,35,41, 45],
'Mark2':[39, 26, 43, 35]})
DF2 = pd.DataFrame({'Mark1':[43,42,39,40],’
Mark2':[30, 46,33,29]})
print(DF1)
print(DF2)
(i) print(DF1.add(DF2))
(ii) print(DF1.subtract(DF2))
(iii) DF1.rename(columns={‘Mark1’:'M1'}, inplace=True)
(iv) DF1.rename(index = {0: "zero", 1:"one"}, inplace = True)
CBSE QUESTIONS 49
Sangita Panchal
Consider the following dataframe df:
name marks grade
0 Amit 10 C
1 Ravit 23 A
2 Ria 21 A
3 Arnav 11 C
a) Write the command to add a new row to df with the values in data.
data = {'name':'Manan','marks':19,'grade':'c'}
b) Write the command to change the column label marks to score.
c) Write the command to arrange the dataframe in descending order of marks.
d) What will be the output of following:
df [ (df['grade'] == 'A') & (df['marks'] >= 20) ]
CBSE QUESTIONS 50
Sangita Panchal
Consider the following dataframe df:
name marks grade
0 Amit 10 C
1 Ravit 23 A
2 Ria 21 A
3 Arnav 11 C
a) Write the command to add a new row to df with the values in data.
data = {'name':'Manan','marks':19,'grade':'c'}
b) Write the command to change the column label marks to score.
c) Write the command to arrange the dataframe in descending order of marks.
d) What will be the output of following:
df [ (df['grade'] == 'A') & (df['marks'] >= 20) ]
df.append(data,ignore_index=True)
df.rename({'marks':'score'},axis=1)
df.sort_values('marks',ascending=False)
name marks grade
1 Ravit 23 A
2 Ria 21 A
CBSE QUESTIONS 51
Sangita Panchal
Consider the following dataframe:
import pandas as pd
df_sales = pd.DataFrame( {"Qtr1":[20, 40, 50, 44, 10],
"Qtr2":[58, 25, 54, 30, 29],
"Qtr3":[20, 16, 70, 36, 82],
"Qtr4":[14, 37, 17, 20, 60]} )
a) Write the command to display the top three rows from above data frame.
b) Write the command to display all the rows of the columns ‘Qtr1’ and ‘Qtr2’.
c) What will be the output of the following?
df_sales.loc[ :2 , ‘Qtr3': ]
CBSE QUESTIONS 52
Sangita Panchal
Consider the following dataframe:
import pandas as pd
df_sales = pd.DataFrame( {"Qtr1":[20, 40, 50, 44, 10],
"Qtr2":[58, 25, 54, 30, 29],
"Qtr3":[20, 16, 70, 36, 82],
"Qtr4":[14, 37, 17, 20, 60]} )
a) Write the command to display the top three rows from above dataframe.
b) Write the command to display all the rows of the columns ‘Qtr1’ and ‘Qtr2’.
c) What will be the output of the following?
df_sales.loc[ :2 , ‘Qtr3': ]
df_sales.head(3)
df_sales[ [‘Qtr1',’Qtr2'] ]
Qtr3 Qtr4
0 20 14
1 16 37
2 70 17
DataFrame in Python Pandas

More Related Content

What's hot

Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in pythoneShikshak
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide shareDevashish Kumar
 
Arrays in python
Arrays in pythonArrays in python
Arrays in pythonmoazamali28
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in pythonhydpy
 
Date and Time Module in Python | Edureka
Date and Time Module in Python | EdurekaDate and Time Module in Python | Edureka
Date and Time Module in Python | EdurekaEdureka!
 
pandas - Python Data Analysis
pandas - Python Data Analysispandas - Python Data Analysis
pandas - Python Data AnalysisAndrew Henshaw
 
SQL - Structured query language introduction
SQL - Structured query language introductionSQL - Structured query language introduction
SQL - Structured query language introductionSmriti Jain
 
Functions in python
Functions in pythonFunctions in python
Functions in pythoncolorsof
 
Looping statement in python
Looping statement in pythonLooping statement in python
Looping statement in pythonRaginiJain21
 
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...Edureka!
 
Looping Statements and Control Statements in Python
Looping Statements and Control Statements in PythonLooping Statements and Control Statements in Python
Looping Statements and Control Statements in PythonPriyankaC44
 

What's hot (20)

Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
 
Python list
Python listPython list
Python list
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
 
List in Python
List in PythonList in Python
List in Python
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in python
 
Python programming : List and tuples
Python programming : List and tuplesPython programming : List and tuples
Python programming : List and tuples
 
Date and Time Module in Python | Edureka
Date and Time Module in Python | EdurekaDate and Time Module in Python | Edureka
Date and Time Module in Python | Edureka
 
pandas - Python Data Analysis
pandas - Python Data Analysispandas - Python Data Analysis
pandas - Python Data Analysis
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 
Python Modules
Python ModulesPython Modules
Python Modules
 
Set methods in python
Set methods in pythonSet methods in python
Set methods in python
 
SQL - Structured query language introduction
SQL - Structured query language introductionSQL - Structured query language introduction
SQL - Structured query language introduction
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 
Looping statement in python
Looping statement in pythonLooping statement in python
Looping statement in python
 
8 python data structure-1
8 python data structure-18 python data structure-1
8 python data structure-1
 
Pandas Series
Pandas SeriesPandas Series
Pandas Series
 
Python basic
Python basicPython basic
Python basic
 
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
 
Looping Statements and Control Statements in Python
Looping Statements and Control Statements in PythonLooping Statements and Control Statements in Python
Looping Statements and Control Statements in Python
 

Similar to DataFrame in Python Pandas

R Cheat Sheet – Data Management
R Cheat Sheet – Data ManagementR Cheat Sheet – Data Management
R Cheat Sheet – Data ManagementDr. Volkan OBAN
 
fINAL Lesson_5_Data_Manipulation_using_R_v1.pptx
fINAL Lesson_5_Data_Manipulation_using_R_v1.pptxfINAL Lesson_5_Data_Manipulation_using_R_v1.pptx
fINAL Lesson_5_Data_Manipulation_using_R_v1.pptxdataKarthik
 
Data Wrangling with dplyr and tidyr Cheat Sheet
Data Wrangling with dplyr and tidyr Cheat SheetData Wrangling with dplyr and tidyr Cheat Sheet
Data Wrangling with dplyr and tidyr Cheat SheetDr. Volkan OBAN
 
Basic R Data Manipulation
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data ManipulationChu An
 
Introduction to matlab lecture 3 of 4
Introduction to matlab lecture 3 of 4Introduction to matlab lecture 3 of 4
Introduction to matlab lecture 3 of 4Randa Elanwar
 
Pandas,scipy,numpy cheatsheet
Pandas,scipy,numpy cheatsheetPandas,scipy,numpy cheatsheet
Pandas,scipy,numpy cheatsheetDr. Volkan OBAN
 
R Programming.pptx
R Programming.pptxR Programming.pptx
R Programming.pptxkalai75
 
Class program and uml in c++
Class program and uml in c++Class program and uml in c++
Class program and uml in c++Osama Al-Mohaia
 
R reference card short
R reference card shortR reference card short
R reference card shortShekar Manick
 

Similar to DataFrame in Python Pandas (20)

R Cheat Sheet – Data Management
R Cheat Sheet – Data ManagementR Cheat Sheet – Data Management
R Cheat Sheet – Data Management
 
Introduction to r
Introduction to rIntroduction to r
Introduction to r
 
R Programming Homework Help
R Programming Homework HelpR Programming Homework Help
R Programming Homework Help
 
fINAL Lesson_5_Data_Manipulation_using_R_v1.pptx
fINAL Lesson_5_Data_Manipulation_using_R_v1.pptxfINAL Lesson_5_Data_Manipulation_using_R_v1.pptx
fINAL Lesson_5_Data_Manipulation_using_R_v1.pptx
 
Lecture 9.pptx
Lecture 9.pptxLecture 9.pptx
Lecture 9.pptx
 
R for Statistical Computing
R for Statistical ComputingR for Statistical Computing
R for Statistical Computing
 
Learn Matlab
Learn MatlabLearn Matlab
Learn Matlab
 
interenship.pptx
interenship.pptxinterenship.pptx
interenship.pptx
 
Data Wrangling with dplyr and tidyr Cheat Sheet
Data Wrangling with dplyr and tidyr Cheat SheetData Wrangling with dplyr and tidyr Cheat Sheet
Data Wrangling with dplyr and tidyr Cheat Sheet
 
Data Management in R
Data Management in RData Management in R
Data Management in R
 
20100528
2010052820100528
20100528
 
20100528
2010052820100528
20100528
 
Basic R Data Manipulation
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data Manipulation
 
Introduction to matlab lecture 3 of 4
Introduction to matlab lecture 3 of 4Introduction to matlab lecture 3 of 4
Introduction to matlab lecture 3 of 4
 
Pandas,scipy,numpy cheatsheet
Pandas,scipy,numpy cheatsheetPandas,scipy,numpy cheatsheet
Pandas,scipy,numpy cheatsheet
 
R Programming.pptx
R Programming.pptxR Programming.pptx
R Programming.pptx
 
Programming in R
Programming in RProgramming in R
Programming in R
 
Class program and uml in c++
Class program and uml in c++Class program and uml in c++
Class program and uml in c++
 
Basic Analysis using Python
Basic Analysis using PythonBasic Analysis using Python
Basic Analysis using Python
 
R reference card short
R reference card shortR reference card short
R reference card short
 

Recently uploaded

ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxNikitaBankoti2
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesShubhangi Sonawane
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 

Recently uploaded (20)

ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 

DataFrame in Python Pandas

  • 2. Data Handling using Pandas – 1 Data Frames: creation - from dictionary of Series, list of dictionaries, Text/CSV files; display; iteration; Operations on rows and columns: add, select, delete, rename; Head and Tail functions; Indexing using Labels, Boolean Indexing; Importing/Exporting Data between CSV files and Data Frames. 1 Sangita Panchal
  • 3. Pandas DataFrame  A pandas DataFrame is a two-dimensional array.  Data is aligned in rows and columns. The general format: pandas. DataFrame (data, index, columns, dtype) data: It is like ndarray, series, list, dict, constants or another DataFrame. index: It is for row label, by default 0..n-1 (np.arange(n)) columns: It is for column label, by default 0…n-1 (np.arange(n)) dtype: data type of each column 2 Sangita Panchal Key Features: • Heterogeneous data • Size Mutable • Data Mutable
  • 4. Creating DataFrame using list 3 Sangita Panchal import pandas as pd D = pd.DataFrame([[10,20],[30,40]]) print(D) 0 1 0 10 20 1 30 40 Default columns Default rows
  • 5. Creating DataFrame with row index and column label 4 Sangita Panchal import pandas as pd data = [[10,20],[30,40]] D = pd.DataFrame(data,columns = ['col1','col1'],index = ['row1','row2']) print(D) col1 col1 row1 10 20 row2 30 40 Column Label Row Label
  • 6. Creating DataFrame using dictionary 5 Sangita Panchal import pandas as pd data = {'Name':['Anu','Sia'],'Marks':[19,25]} D = pd.DataFrame(data,index = [1,2]) The column names are the keys in a dictionary. Rows using index
  • 7. Creating DataFrame from dictionary of Series 6 Sangita Panchal import pandas as pd d = {'one' : pd.Series([10, 20, 30, 40], index =['a', 'b', 'c', 'd']), 'two' : pd.Series([10, 20, 30, 40], index =['a', 'b', 'c', 'd'])} df = pd.DataFrame(d) print(df )
  • 8. Creating DataFrame from list of dictionary 7 Sangita Panchal import pandas as pd data = [{'b': 2, 'c':3}, {'a': 10, 'b': 20, 'c': 30}] df = pd.DataFrame(data, index =['first', 'second']) df = pd.DataFrame(d) print(df )
  • 9. Writing DataFrame to csv file 8 Sangita Panchal import pandas as pd data = {'Name':['Anu','Sia'],'Marks':[19,25]} D = pd.DataFrame(data,index = [1,2]) print(D) D.to_csv("E:stu.csv") Writing DataFrame data to Excel file
  • 10. Creating DataFrame using text/csv file 9 Sangita Panchal import pandas as pd >>> data = pd.read_csv("C:/Users/admin/Desktop/stock.csv") >>> print(data) No Name Qty 0 1 Pencil 12 1 2 Pen 20 2 3 Eraser 17 3 4 Sharpner 10 Create .csv file using  Notepad  Excel Read data in Excel file to DataFrame
  • 11. Iteration in DataFrame 10 Sangita Panchal import pandas as pd Data = [[1006,1008],['A1','B1'],[23000,19000]] D = pd.DataFrame(Data) print(D) print(D.shape, D.size) for row in range(D.shape[0]): print("Row ",row,"Columns",end = " ") for col in range(D.shape[1]): print(D[col][row], end = " ") print() 0 1 0 1006 1008 1 A1 B1 2 23000 19000 (3, 2) 6 Row 0 Columns 1006 1008 Row 1 Columns A1 B1 Row 2 Columns 23000 1900
  • 12. Iteration in DataFrame using iterrows() 11 Sangita Panchal import pandas as pd Data = {'Name': ['Amy','Anu','Jia’], 'Total':[45,32,76]} D = pd.DataFrame(Data) print(D) for i in D.iterrows(): print(i) Name Total 0 Amy 45 1 Anu 32 2 Jia 76 (0, Name Amy Total 45 Name: 0, dtype: object) (1, Name Anu Total 32 Name: 1, dtype: object) (2, Name Jia Total 76 Name: 2, dtype: object) iterrows() - used for iterating over the rows as (index, series) pairs.
  • 13. Add column, Insert column 12 Sangita Panchal import pandas as pd data = {'Name':['Anu','Sia'],'M1':[19,25]} D = pd.DataFrame(data,index = [1,2]) print(D) D['M2'] = [13,34] D['Total'] = D['M1']+D['M2'] print("Added columns ", D, sep = "n") print(“Inserted columns”) D.insert(1,"Remarks",[‘A',’B']) print(D) Name M1 1 Anu 19 2 Sia 25 Added columns Name M1 M2 Total 1 Anu 19 13 32 2 Sia 25 34 59 Inserted column Name Remarks M1 M2 Total 1 Anu A 19 13 32 2 Sia B 25 34 59
  • 14. Add Row at the end 13 Sangita Panchal import pandas as pd data = {'Name':['Anu','Sia'],'M1':[19,25], 'M2': [13,34], 'Total':[32, 59]} D = pd.DataFrame(data,index = [1,2]) print(D) D.loc[3] = ['Jia',23,24,47] print("Added Row using loc",D, sep = "n") Name M1 M2 Total 1 Anu 19 13 32 2 Sia 25 34 59 Added Row Name M1 M2 Total 1 Anu 19 13 32 2 Sia 25 34 59 3 Jia 23 24 47 D = D.append({'Name':'Somy','M1':21,'M2':19,'Total':40},ignore_index = True) print("Added Row using append",D, sep = "n") Added Row using append Name M1 M2 Total 0 Anu 19 13 32 1 Sia 25 34 59 2 Jia 23 24 47 3 Somy 21 19 40
  • 15. Select Column 14 Sangita Panchal print(D['Total']) print(D[['M1', 'M2']]) print(D.Name) Name M1 M2 Total 0 Anu 19 13 32 1 Sia 25 34 59 2 Jia 23 24 47 0 32 1 59 2 47 Name: Total, dtype: int64 M1 M2 0 19 13 1 25 34 2 23 24 0 Anu 1 Sia 2 Jia Name: Name, dtype: object You can select a column using dot notation or using square brackets.
  • 16. Delete column, row 15 Sangita Panchal D.drop(columns = ['Name’], inplace = True) print(D) D.drop(columns = [‘M1’,’M2’], inplace = True) print(D) D.drop(index = [0,1],inplace = True) print(D) Name M1 M2 Total 0 Anu 19 13 32 1 Sia 25 34 59 2 Jia 23 24 47 M1 M2 Total 0 19 13 32 1 25 34 59 2 23 24 47 Total 0 32 1 59 2 47 Total 2 47 To delete single column - del, pop(), drop() To delete multiple columns - drop() To delete single / multiple rows - drop()
  • 17. Rename column 16 Sangita Panchal D.rename(columns = {'Name':'StuName'},inplace = True) print(D) D.rename(index = {0:'zero',1:'one’},columns = {"Total":"T"}, inplace = True) print(D) Name M1 M2 Total 0 Anu 19 13 32 1 Sia 25 34 59 2 Jia 23 24 47 StuName M1 M2 Total 0 Anu 19 13 32 1 Sia 25 34 59 2 Jia 23 24 47 StuName M1 M2 T zero Anu 19 13 32 one Sia 25 34 59 2 Jia 23 24 47 Change column name Change row index
  • 18. Rename column label/row index 16a Sangita Panchal print(D.columns) print(D.index) D.columns = [‘a’,’b’,’c’,’d’] D.index = [1,2,3] StuName M1 M2 T zero Anu 19 13 32 one Sia 25 34 59 2 Jia 23 24 47 Index(['StuName', 'M1', 'M2', 'T'], dtype='object') Index(['zero', 'one', 2], dtype='object’) a b c d zero Anu 19 13 32 one Sia 25 34 59 2 Jia 23 24 47 a b c d 1 Anu 19 13 32 2 Sia 25 34 59 3 Jia 23 24 47
  • 19. Head and Tail operation 17 Sangita Panchal print("Top 2 rows",D.head(2),sep = "n") print("Bottom 2 rows",D.tail(2), sep = "n") Name M1 M2 Total 0 Anu 19 13 32 1 Sia 25 34 59 2 Jia 23 24 47 Top 2 rows Name M1 M2 Total 0 Anu 19 13 32 1 Sia 25 34 59 Bottom 2 rows Name M1 M2 Total 1 Sia 25 34 59 2 Jia 23 24 47 Note: The default of head() and tail() is 5. print(D.head() )displays top 5 rows. print(D.tail()) displays bottom 5 rows.
  • 20. Indexing using Label /Position 18 Sangita Panchal There are 3 ways of indexing: 1. DF[“col1”] DF[[“col1”,”col2”]] 2. DF.loc[“col1”] DF.loc[[“col1”,”col2”]] DF.loc[[“row1”,”row2”],[“col1”,”col2”]] DF.loc[ : :, : :] 3. DF.iloc[3] DF.iloc[[1,3,4]] DF.iloc[[3,4],[1,2]] DF.iloc[ : : , : :] For column only For position (Default index ] Used for Labels
  • 21. Indexing using Label / Position 19 Sangita Panchal print("Select Name column ", D['Name'],sep = "n") print("Select Row and column ",D.iloc[::2,1:3],sep = "n") print("Select second row",D.iloc[1,:],sep = "n") Name M1 M2 Total 0 Anu 19 13 32 1 Sia 25 34 59 2 Jia 23 24 47 Select Name column 0 Anu 1 Sia 2 Jia Name: Name, dtype: object Select Row and column M1 M2 0 19 13 2 23 24 Select second row Name Sia M1 25 M2 34 Total 59 Name: 1, dtype: object47
  • 22. Indexing using Label / Position 20 Sangita Panchal print(D["Total"]) print(D.iloc[[1,2],[0,1]]) print(D.loc[[1,2],["Name","M1"]]) print(D.iloc[0:3:2,::2]) print(D.loc[0:3:2,"Name":"Total":2])
  • 23. Indexing using Label / Position 21 Sangita Panchal print(D.Total > 50) print((D.M1 > 20) & (D.M2 > 20)) print((D.M1 > 20) | (D.M2 > 20)) print(~(D.Total> 40)) ~ NOT & AND | OR
  • 24. Indexing using Label / Position 22 Sangita Panchal print(D[(D.M1 > 20) & (D.M2 > 20)]) print(D[(D.M1 > 20) | (D.M2 > 20)]) print(D[~(D.Total> 40)])
  • 25. Append two DataFrame and sort 23 Sangita Panchal import pandas as pd data = {'Name':['A','B'],'Total':[89,78]} D = pd.DataFrame(data) print(D) data1 = {'Name':['C','D'],'Total':[78,91]} D1 = pd.DataFrame(data1) print(D1) D2 = D.append(D1) print(D2) print(D2.sort_values('Total', ascending = False))
  • 26. max, min, sum, mean, count 24 Sangita Panchal print(D.max()) print(D.count(axis = 1)) print(D['Total'].min()) print(D[['M1','M2']].sum()) print(D[['M1','M2','Total']].mean()) M1 42.0 M2 71.0 dtype: float64 M1 21.000000 M2 23.666667 Total 46.000000 dtype: float64 Name M1 M2 Total 0 Anu 19.0 13 32 1 Sia NaN 34 59 2 Sid 23.0 24 47 Name Sid M1 23 M2 34 Total 59 dtype: object 0 4 1 3 2 4 dtype: int64 32 By default, these functions are applied to rows (axis =0). For columns, axis = 1 is mandatory. Note: D.max() cann be written as D.max(0) D.count(axis = 1) can be written as D.count(1)
  • 27. CBSE QUESTIONS 25 Sangita Panchal 1. In a DataFrame, Axis= 1 represents the_____________ elements. 2. In Pandas the function used to check for null values in a DataFrame is ________ 3. Consider the following DataFrame df and answer any four questions from (i)-(v) rollno name UT1 UT2 UT3 UT4 1 Prerna Singh 24 24 20 22 2 Manish Arora 18 17 19 22 3 Tanish Goel 20 22 18 24 4 Falguni Jain 22 20 24 20 5 Kanika Bhatnagar 15 20 18 22 6 Ramandeep Kaur 20 15 22 24
  • 28. CBSE QUESTIONS 26 Sangita Panchal 1. In a DataFrame, Axis= 1 represents the_____________ elements. 2. In Pandas the function used to check for null values in a DataFrame is ________ 3. Consider the following DataFrame df and answer any four questions from (i)-(v) rollno name UT1 UT2 UT3 UT4 1 Prerna Singh 24 24 20 22 2 Manish Arora 18 17 19 22 3 Tanish Goel 20 22 18 24 4 Falguni Jain 22 20 24 20 5 Kanika Bhatnagar 15 20 18 22 6 Ramandeep Kaur 20 15 22 24 column isnull()
  • 29. CBSE QUESTIONS 27 Sangita Panchal (I) Write down the command that will give the following output: a. print(df.max) b. print(df.max()) c. print(df.max(axis=1)) d. print(df.max, axis=1)
  • 30. CBSE QUESTIONS 28 Sangita Panchal (I) Write down the command that will give the following output: a. print(df.max) b. print(df.max()) c. print(df.max(axis=1)) d. print(df.max, axis=1) print(df.max())
  • 31. CBSE QUESTIONS 29 Sangita Panchal The teacher needs to know the marks scored by the student with roll number 4. Help her to identify the correct set of statement/s from the given options : a. df1=df[df[‘rollno’]==4] ; print(df1) b. df1=df[rollno==4] ; print(df1) c. df1=df[df.rollno=4] ; print(df1) d. df1=df[df.rollno==4] ; print(df1) v. Ms. Sharma, the class teacher wants to add a new column, the scores of Grade with the values, ‘ A’, ‘B’, ‘A’, ‘A’, ‘B’, ‘A’ ,to the DataFrame. Help her choose the command to do so: a. df.column=[’A’,’B’,’A’,’A’,’B’,’A’] b. df [‘Grade’]=[’A’,’B’,’A’,’A’,’B’,’A’] c. df.loc[‘Grade’]= [’A’,’B’,’A’,’A’,’B’,’A’] d. Both (b) and (c) are correct
  • 32. CBSE QUESTIONS 30 Sangita Panchal The teacher needs to know the marks scored by the student with roll number 4. Help her to identify the correct set of statement/s from the given options : a. df1=df[df[‘rollno’]==4] ; print(df1) b. df1=df[rollno==4] ; print(df1) c. df1=df[df.rollno=4] ; print(df1) d. df1=df[df.rollno==4] ; print(df1) v. Ms. Sharma, the class teacher wants to add a new column, the scores of Grade with the values, ‘ A’, ‘B’, ‘A’, ‘A’, ‘B’, ‘A’ ,to the DataFrame. Help her choose the command to do so: a. df.column=[’A’,’B’,’A’,’A’,’B’,’A’] b. df [‘Grade’]=[’A’,’B’,’A’,’A’,’B’,’A’] c. df.loc[‘Grade’]= [’A’,’B’,’A’,’A’,’B’,’A’] d. Both (b) and (c) are correct a and d b
  • 33. CBSE QUESTIONS 31 Sangita Panchal (iii) Which of the following statement/s will give the exact number of values in each column of the dataframe? i. print(df.count()) ii. print(df.count(0)) iii. print(df.count) iv. print(df.count(axis=’index’)) Choose the correct option: a. both (i) and (ii) b. only (ii) c. (i), (ii) and (iii) d. (i), (ii) and (iv) iv. Which of the following command will display the column labels of the DataFrame? a. print(df.columns()) b. print(df.column()) c. print(df.column) d. print(df.columns)
  • 34. CBSE QUESTIONS 32 Sangita Panchal (iii) Which of the following statement/s will give the exact number of values in each column of the dataframe? i. print(df.count()) ii. print(df.count(0)) iii. print(df.count) iv. print(df.count(axis=’index’)) Choose the correct option: a. both (i) and (ii) b. only (ii) c. (i), (ii) and (iii) d. (i), (ii) and (iv) iv. Which of the following command will display the column labels of the DataFrame? a. print(df.columns()) b. print(df.column()) c. print(df.column) d. print(df.columns) a d
  • 35. CBSE QUESTIONS 33 Sangita Panchal Consider the following DataFrame, classframe Write commands to : i. Add a new column ‘Activity’ to the Dataframe ii. Add a new row with values ( 5 , Mridula ,X, F , 9.8, Science)
  • 36. CBSE QUESTIONS 34 Sangita Panchal Consider the following DataFrame, classframe Write commands to : i. Add a new column ‘Activity’ to the Dataframe ii. Add a new row with values ( 5 , Mridula ,X, F , 9.8, Science) i. classframe[‘Activity’] = [‘a’,’b’,’c’,’d’] ii. classframe.loc[‘St5’] = [5, ‘Mridula’,’X’,’F’,9.8,’Science’]
  • 37. CBSE QUESTIONS 35 Sangita Panchal Write a program in Python Pandas to create the following DataFrame batsman from a Dictionary: Perform the following operations on the DataFrame : 1)Add both the scores of a batsman and assign to column “Total” 2)Display the highest score in both Score1 and Score2 of the DataFrame. 3. Display the DataFrame
  • 38. CBSE QUESTIONS 36 Sangita Panchal Write a program in Python Pandas to create the following DataFrame batsman from a Dictionary: Perform the following operations on the DataFrame : 1)Add both the scores of a batsman and assign to column “Total” 2)Display the highest score in both Score1 and Score2 of the DataFrame. 3. Display the DataFrame import pandas as pd] data = {‘B_NO’: [1,2,3,4], ‘Name’:[‘Sunil Pillai’,’Gaurav Sharma’, ‘Piyush Goel’,’Kartik Thakur’], ‘Score1’:[90,65,70,80], ’Score2’:[80,45,90,76]} batsman = pd.DataFrame(data) i. batsman[‘Total’] = batsman[‘Score1’] + batsman[‘Score2’] ii. print(batsman[[‘Score1’,’Score2’]].max()) iii. print(batsman)
  • 39. CBSE QUESTIONS 37 Sangita Panchal Consider the dataframe df, write the output of the following: Eng Math Sci 2017 75 95 75 2018 99 99 NaN 2019 98 95 95 a. print(df[‘Eng’].min()) b. print(df[[‘Eng’,’Sci’]].count()) c. print(df.loc[2018].max()) d. print(df.loc[2018 : 2020,’Maths’:’Sci’].max())
  • 40. CBSE QUESTIONS 38 Sangita Panchal Consider the dataframe df, write the output of the following: Eng Math Sci 2017 75 95 75 2018 99 99 NaN 2019 98 95 95 a. print(df[‘Eng’].min()) b. print(df[[‘Eng’,’Sci’]].count()) c. print(df.loc[2018].max()) d. print(df.loc[2018 : 2019,’Maths’:’Sci’].max()) a. 75 b. Eng 3 Sci 2 c. 99.0 d. Math 99.0 Sci 95.0
  • 41. CBSE QUESTIONS 39 Sangita Panchal (a) The ______________ method in Pandas can be used to delete rows or columns. (b) The code to display the last three rows of the dataframe df is ___________. (c) Consider a dataframe DF Ecode Ename 1 Sarika 2 Monica 3 Mehak Write the command to add a new column named “Salary” from the list Sal = [15000, 18000, 16000] Ecode Salary Ename 1 15000 Sarika 2 18000 Monica 3 16000 Mehak
  • 42. CBSE QUESTIONS 40 Sangita Panchal (a) The ______________ method in Pandas can be used to delete rows or columns. (b) The code to display the last three rows of the dataframe df is ___________. (c) Consider a dataframe DF Ecode Ename 1 Sarika 2 Monica 3 Mehak Write the command to add a new column named “Salary” from the list Sal = [15000, 18000, 16000] Ecode Salary Ename 1 15000 Sarika 2 18000 Monica 3 16000 Mehak drop() df.tail(3) DF.insert(1,”salary”, Sal) or DF.insert(loc = 1, column = “Salary”, value = Sal)
  • 43. CBSE QUESTIONS 41 Sangita Panchal (d) Write a Python code to create a dataframe from the list given below: [‘E001’, ‘Govind’, 35000],[‘E002’, ‘Jatin’, 43000], [‘E003’, ‘Deepak’, 38000] Give the column heading as EmpCode, EmpName and EmpSalary. Also give the index as 1, 2 and 3. import pandas as pd data = [[‘E001’, ‘Govind’, 35000],[‘E002’, ‘Jatin’, 43000], [‘E003’, ‘Deepak’, 38000]] df = pd.DataFrame(data, columns = [‘EmpCode’, ‘EmpName’, ‘EmpSalary’], index = [1, 2, 3]) print(df)
  • 44. CBSE QUESTIONS 42 Sangita Panchal (e) Consider te following dataframe, and answer the questions given below: import pandas as pd df = pd.DataFrame({“Sub1”:[56, 78, 67, 98, 65], “Sub2”: [87, 92, 76, 56, 69], “Sub3”:[65, 64, 74, 72, 81], “Sub4”:[90, 87, 56, 66, 86]}) (i) Write the code to find the mean value row wise and column wise. (ii) Write the code to find the sum value row wise. (i) print(df.mean(axis = 1)) print(df.mean(axis = 0)) (ii) print(df.sum(axis = 1))
  • 45. CBSE QUESTIONS 43 Sangita Panchal (f) Given a dataframe DF of marks obtained by 100 students as shown below: Highest Lowest Average English 95 56 67.8 Maths 100 65 59.8 Physics 97 68 69.8 Chemistry 99 54 79.8 CompSc 100 82 90.8 (i) Write command to compute sum of every column of the dataframe. (ii) Write command to compute mean of Average column. (iii) Write command to compute average of Highest and Lowest columns
  • 46. CBSE QUESTIONS 44 Sangita Panchal (f) Given a dataframe DF of marks obtained by 100 students as shown below: SubjectHighest LowestAverage English 95 56 67.8 Maths 100 65 59.8 Physics 97 68 69.8 Chemistry 99 54 79.8 CompSc 100 82 90.8 (i) Write command to compute sum of every column of the dataframe. (ii) Write command to compute mean of Average column. (iii) Write command to compute average of Highest and Lowest columns DF.sum() DF[‘Average’].mean() DF[[‘Highest’,’Lowest’]].mean()
  • 47. CBSE QUESTIONS 45 Sangita Panchal (g) Find the output of the following code: import pandas as pd data = [{'a': 10, 'b': 20},{'a': 6, 'b1': 32, 'c': 22}] df1 = pd.DataFrame(data, index=['first', 'second'], columns=['a', 'b', ‘c’]) df2 = pd.DataFrame(data, index=['first', 'second'], columns=['a', 'b1']) print(df1) print(df2)
  • 48. CBSE QUESTIONS 46 Sangita Panchal (g) Find the output of the following code: import pandas as pd data = [{'a': 10, 'b': 20},{'a': 6, 'b1': 32, 'c': 22}] df1 = pd.DataFrame(data, index=['first', 'second'], columns=['a', 'b', ‘c’]) df2 = pd.DataFrame(data, index=['first', 'second'], columns=['a', 'b1']) print(df1) print(df2) a b c first 10 20 NaN second 6 NaN 22 a b 1 first 10 NaN second 6 32
  • 49. CBSE QUESTIONS 47 Sangita Panchal (h) Write the code in pandas to create the following dataframes: df1 df2 a. Write a command to add dataframe DF1 and DF2. b. Write a command to subtract datafram DF2 from DF1. c. Write a command to rename DF1 column Marks1 to M1. d. Write a command to rename DF1 row index 0 to zero, 1 to one.
  • 50. CBSE QUESTIONS 48 Sangita Panchal (h) Write the code in pandas to create the following dataframes: df1 df2 a. Write a command to add dataframe DF1 and DF2. b. Write a command to subtract datafram DF2 from DF1. c. Write a command to rename DF1 column Marks1 to M1. d. Write a command to rename DF1 row index 0 to zeor, 1 to one. import pandas as pd DF1 = pd.DataFrame({'Mark1':[49,35,41, 45], 'Mark2':[39, 26, 43, 35]}) DF2 = pd.DataFrame({'Mark1':[43,42,39,40],’ Mark2':[30, 46,33,29]}) print(DF1) print(DF2) (i) print(DF1.add(DF2)) (ii) print(DF1.subtract(DF2)) (iii) DF1.rename(columns={‘Mark1’:'M1'}, inplace=True) (iv) DF1.rename(index = {0: "zero", 1:"one"}, inplace = True)
  • 51. CBSE QUESTIONS 49 Sangita Panchal Consider the following dataframe df: name marks grade 0 Amit 10 C 1 Ravit 23 A 2 Ria 21 A 3 Arnav 11 C a) Write the command to add a new row to df with the values in data. data = {'name':'Manan','marks':19,'grade':'c'} b) Write the command to change the column label marks to score. c) Write the command to arrange the dataframe in descending order of marks. d) What will be the output of following: df [ (df['grade'] == 'A') & (df['marks'] >= 20) ]
  • 52. CBSE QUESTIONS 50 Sangita Panchal Consider the following dataframe df: name marks grade 0 Amit 10 C 1 Ravit 23 A 2 Ria 21 A 3 Arnav 11 C a) Write the command to add a new row to df with the values in data. data = {'name':'Manan','marks':19,'grade':'c'} b) Write the command to change the column label marks to score. c) Write the command to arrange the dataframe in descending order of marks. d) What will be the output of following: df [ (df['grade'] == 'A') & (df['marks'] >= 20) ] df.append(data,ignore_index=True) df.rename({'marks':'score'},axis=1) df.sort_values('marks',ascending=False) name marks grade 1 Ravit 23 A 2 Ria 21 A
  • 53. CBSE QUESTIONS 51 Sangita Panchal Consider the following dataframe: import pandas as pd df_sales = pd.DataFrame( {"Qtr1":[20, 40, 50, 44, 10], "Qtr2":[58, 25, 54, 30, 29], "Qtr3":[20, 16, 70, 36, 82], "Qtr4":[14, 37, 17, 20, 60]} ) a) Write the command to display the top three rows from above data frame. b) Write the command to display all the rows of the columns ‘Qtr1’ and ‘Qtr2’. c) What will be the output of the following? df_sales.loc[ :2 , ‘Qtr3': ]
  • 54. CBSE QUESTIONS 52 Sangita Panchal Consider the following dataframe: import pandas as pd df_sales = pd.DataFrame( {"Qtr1":[20, 40, 50, 44, 10], "Qtr2":[58, 25, 54, 30, 29], "Qtr3":[20, 16, 70, 36, 82], "Qtr4":[14, 37, 17, 20, 60]} ) a) Write the command to display the top three rows from above dataframe. b) Write the command to display all the rows of the columns ‘Qtr1’ and ‘Qtr2’. c) What will be the output of the following? df_sales.loc[ :2 , ‘Qtr3': ] df_sales.head(3) df_sales[ [‘Qtr1',’Qtr2'] ] Qtr3 Qtr4 0 20 14 1 16 37 2 70 17