0% found this document useful (0 votes)
11 views10 pages

Iteration Over DataFrame

DataFrame

Uploaded by

Jayabharathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views10 pages

Iteration Over DataFrame

DataFrame

Uploaded by

Jayabharathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Iteration over DataFrame

 iterrows()
 iteritems()

iterrows()

iterrows() method iterates over a DataFramerow wise,where each horizontal


subset is in form of(RowIndex ,series) where series contains all column values that
row - index.

iteritems()

iteritems() method iterates over a DataFrame column wise,where each


vertical subset is in the form of (ColumnIndex,series) where series contains all
column values for that column- index.

Code

import pandas as pd

data = {

'Name': ['A', 'B', 'C','D','E','F','G'],

'Age': [25, 30, 35,24,35,22,34],

'City': ['Delhi', 'Goa', 'Mumbai','AP','MP','TN','Goa']

df = pd.DataFrame(data,
index=['Stud1','Stud2','Stud3','Stud4','Stud5','Stud6','Stud7'])

OUTPUT

print(df) Name Age City

Stud1 A 25 Delhi

1
Stud2 B 30 Goa

Stud3 C 35 Mumbai

Stud4 D 24 AP

Stud5 E 35 MP

Stud6 F 22 TN

Stud7 G 34 Goa

for (i) in df.iterrows(): ('Stud1', Name A

print(i) Age 25

City Delhi

Name: Stud1, dtype: object)

('Stud2', Name B

Age 30

City Goa

Name: Stud2, dtype: object)

('Stud3', Name C

Age 35

City Mumbai

Name: Stud3, dtype: object)

('Stud4', Name D

Age 24

City AP

2
Name: Stud4, dtype: object)

('Stud5', Name E

Age 35

City MP

Name: Stud5, dtype: object)

('Stud6', Name F

Age 22

City TN

Name: Stud6, dtype: object)

('Stud7', Name G

Age 34

City Goa

Name: Stud7, dtype: object)

for (index,data) in df.iterrows(): at index: Stud1

print("at index:",index) value: Name A

print("value:",data) Age 25

City Delhi

Name: Stud1, dtype: object

at index: Stud2

value: Name B

3
Age 30

City Goa

Name: Stud2, dtype: object

at index: Stud3

value: Name C

Age 35

City Mumbai

Name: Stud3, dtype: object

at index: Stud4

value: Name D

Age 24

City AP

Name: Stud4, dtype: object

at index: Stud5

value: Name E

Age 35

City MP

Name: Stud5, dtype: object

at index: Stud6

value: Name F

Age 22

4
City TN

Name: Stud6, dtype: object

at index: Stud7

value: Name G

Age 34

City Goa

Name: Stud7, dtype: object

for i in data: at index: Stud1

print("inner for loop",i) value: Name A

j=0 Age 25

for i in data: City Delhi

print("index:",j,"data:",i) Name: Stud1, dtype: object

j+=1 inner for loop A

inner for loop 25

inner for loop Delhi

index: 0 data: A

index: 1 data: 25

index: 2 data: Delhi

at index: Stud2

value: Name B

Age 30

5
City Goa

Name: Stud2, dtype: object

inner for loop B

inner for loop 30

inner for loop Goa

index: 0 data: B

index: 1 data: 30

index: 2 data: Goa

at index: Stud3

value: Name C

Age 35

City Mumbai

Name: Stud3, dtype: object

inner for loop C

inner for loop 35

inner for loop Mumbai

index: 0 data: C

index: 1 data: 35

index: 2 data: Mumbai

at index: Stud4

value: Name D

6
Age 24

City AP

Name: Stud4, dtype: object

inner for loop D

inner for loop 24

inner for loop AP

index: 0 data: D

index: 1 data: 24

index: 2 data: AP

at index: Stud5

value: Name E

Age 35

City MP

Name: Stud5, dtype: object

inner for loop E

inner for loop 35

inner for loop MP

index: 0 data: E

index: 1 data: 35

index: 2 data: MP

at index: Stud6

7
value: Name F

Age 22

City TN

Name: Stud6, dtype: object

inner for loop F

inner for loop 22

inner for loop TN

index: 0 data: F

index: 1 data: 22

index: 2 data: TN

at index: Stud7

value: Name G

Age 34

City Goa

Name: Stud7, dtype: object

inner for loop G

inner for loop 34

inner for loop Goa

index: 0 data: G

index: 1 data: 34

index: 2 data: Goa

8
for(colindex,data) in df.iteritems(): col index Name

print("col index",colindex) data:

print("data:\n",data) Stud1 A

Stud2 B

Stud3 C

Stud4 D

Stud5 E

Stud6 F

Stud7 G

Name: Name, dtype: object

col index Age

data:

Stud1 25

Stud2 30

Stud3 35

Stud4 24

Stud5 35

Stud6 22

Stud7 34

Name: Age, dtype: int64

col index City

9
data:

Stud1 Delhi

Stud2 Goa

Stud3 Mumbai

Stud4 AP

Stud5 MP

Stud6 TN

Stud7 Goa

Name: City, dtype: object

10

You might also like