df2 = df.iloc[:0] # create dataframe of same structure or columns
import pandas as pd
my_dict={'NAME':['Ravi','Raju','Alex','Ron','King','Jack'],
'ID':[1,2,3,4,5,6],'MATH':[30,40,50,60,70,80],
'ENGLISH':[20,30,40,50,60,70]}
df = pd.DataFrame(data=my_dict)
print(df.iloc[0]) # First rows
Output
NAME Ravi
ID 1
MATH 30
ENGLISH 20
Name: 0, dtype: object
print(df.iloc[1])
Output
NAME Raju
ID 2
MATH 40
ENGLISH 30
Name: 1, dtype: object
print(df.iloc[1,2]) # 40
First two rows
print(df.iloc[:2])
Output
NAME ID MATH ENGLISH
0 Ravi 1 30 20
1 Raju 2 40 30
All records
print(df.iloc[::]) # all rows all columns
print(df.iloc[:,:]) # all rows all columns
print(df.iloc[:,2]) # all rows class column
print(df.iloc[[0]])
output
NAME ID MATH ENGLISH
0 Ravi 1 30 20
print(df.iloc[-1])
NAME Jack
ID 6
MATH 80
ENGLISH 70
Name: 5, dtype: object
A column (MATH) value of last record.
print(df.iloc[-1,2]) # 80
print(df.iloc[2:5]) # range of records
Output NAME ID MATH ENGLISH
2 Alex 3 50 40
3 Ron 4 60 50
4 King 5 70 60
print(df.iloc[[0,-1]]) # first and last records
Output
NAME ID MATH ENGLISH
0 Ravi 1 30 20
5 Jack 6 80 70
print(df.iloc[::2]) alternate rows
print(df.iloc[::3]) # All third rows starting from first
print(df.iloc[lambda x: x.index % 2 == 0]) # All even rows based on Index value
print(df.iloc[lambda x: [4,6,12],:]) # records based on index value
print(df.iloc[lambda x: list(x.id % 2 == 0)]) # All even rows based on a column value
print(df.iloc[lambda x: list(x.mark>90),:]) # records based condition
print(df.iloc[:,[True,False,True,False,True]]) # show all rows with matching columns
To match the number of rows , we have created one new DataFrame with 3 number of row and then used the array.
df2=df.iloc[0:3] # New DataFrame with 3 rows
print(df2.iloc[[True,False,True]]) # Matching rows based on Index value
print(df.iloc[::]) # all rows
print(df.iloc[0]) # first record
print(df.iloc[1]) # second record
print(df.iloc[1,3]) # second record 3rd element ( 85 )
print(df.iloc[:,1]) # All rows name column
print(df.iloc[-1]) # show last record
print(df.iloc[[-1]]) # show last record in full
print(df.iloc[:2]) # first two records
print(df.iloc[:-2]) # All records except last two
print(df.iloc[-2]) # 2nd from last record
print(df.iloc[-2:]) # Last two records
print(df.iloc[2:5]) # range of records, 3rd to 5th
print(df.iloc[[0,-1]]) # first and last row
print(df.iloc[::2]) # alternate rows from DataFrame
print(df.iloc[::3]) # All third rows starting from first
print(df[df['MATH']>=50])
Output is here
NAME ID MATH ENGLISH
2 Alex 3 50 40
3 Ron 4 60 50
4 King 5 70 60
5 Jack 6 80 70
df.iloc[33, df.columns.get_loc('mark')] = 100
Update mark for all students to 100
df.iloc[:, df.columns.get_loc('mark')] = 100
Update mark for all students who got equal to or above 90
df.iloc[df['mark']>=90, df.columns.get_loc('mark')] = 100
Replace the matching string. Here name column is replaced with abcd where it is equal to Kenn Rein.
df.iloc[df['name']=='Kenn Rein',df.columns.get_loc('name')]='abcd'
for i in df.index:
df.iloc[i,3]=51 # update the 4th column with 51
Pandas
Pandas DataFrame
loc - rows and columns by column label
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.