Header Ads Widget

Responsive Advertisement

Pandas provides several more convenient methods for iteration


 Hello friends in this article we learn about how many iteration methods are used in pandas and we learn about only Class 12 CBSE important iteration methods like iterrows() and iteritems(). In this article we learn about with the help of the code in python. You will learn each and every iteration methods easily. Let us start this section.

Pandas provides several more convenient methods for iteration:

With .items() and .iteritems(), you iterate over the columns of a Pandas DataFrame. Each iteration yields a tuple with the name of the column and the column data as a Series object: 

With .iterrows(), you iterate over the rows of a Pandas DataFrame. Each iteration yields a tuple with the name of the row and the row data as a Series object:




import pandas as pd

import numpy as np
data = {
   'name': ['Xavier''Ann''Jana''Yi''Robin''Amal''Nori'],
   'city': ['Mexico City''Toronto''Prague''Shanghai',
          'Manchester''Cairo''Osaka'],
   'age': [41283334383137],
  'py-score': [88.079.081.080.068.061.084.0]}
row_labels = [101102103104105106107]
df = pd.DataFrame(data=data, index=row_labels)
print(df)

for i in df.iterrows():
  print(i,"\n")
  
OUT PUT
       name         city  age  py-score
101  Xavier  Mexico City   41      88.0
102     Ann      Toronto   28      79.0
103    Jana       Prague   33      81.0
104      Yi     Shanghai   34      80.0
105   Robin   Manchester   38      68.0
106    Amal        Cairo   31      61.0
107    Nori        Osaka   37      84.0
(101, name             Xavier
city        Mexico City
age                  41
py-score             88
Name: 101, dtype: object) 

(102, name            Ann
city        Toronto
age              28
py-score         79
Name: 102, dtype: object) 

(103, name          Jana
city        Prague
age             33
py-score        81
Name: 103, dtype: object) 

(104, name              Yi
city        Shanghai
age               34
py-score          80
Name: 104, dtype: object) 

(105, name             Robin
city        Manchester
age                 38
py-score            68
Name: 105, dtype: object) 

(106, name         Amal
city        Cairo
age            31
py-score       61
Name: 106, dtype: object) 

(107, name         Nori
city        Osaka
age            37
py-score       84
Name: 107, dtype: object) 



--------------------------------------------------------------------------------------


import pandas as pd
import numpy as np
data = {
   'name': ['Xavier''Ann''Jana''Yi''Robin''Amal''Nori'],
   'city': ['Mexico City''Toronto''Prague''Shanghai',
          'Manchester''Cairo''Osaka'],
   'age': [41283334383137],
  'py-score': [88.079.081.080.068.061.084.0]}
row_labels = [101102103104105106107]
df = pd.DataFrame(data=data, index=row_labels)
print(df)

for (i,j) in df.iterrows():
  print(i,"\n")
  #for row_data in j:
  print(j,"\n")
 # print("\n")

OutPut of the this code

name city age py-score 101 Xavier Mexico City 41 88.0 102 Ann Toronto 28 79.0 103 Jana Prague 33 81.0 104 Yi Shanghai 34 80.0 105 Robin Manchester 38 68.0 106 Amal Cairo 31 61.0 107 Nori Osaka 37 84.0 101 name Xavier city Mexico City age 41 py-score 88 Name: 101, dtype: object 102 name Ann city Toronto age 28 py-score 79 Name: 102, dtype: object 103 name Jana city Prague age 33 py-score 81 Name: 103, dtype: object 104 name Yi city Shanghai age 34 py-score 80 Name: 104, dtype: object 105 name Robin city Manchester age 38 py-score 68 Name: 105, dtype: object 106 name Amal city Cairo age 31 py-score 61 Name: 106, dtype: object 107 name Nori city Osaka age 37 py-score 84 Name: 107, dtype: object



---------------------------------------------------------------------------------------

import pandas as pd

import numpy as np
data = {
   'name': ['Xavier''Ann''Jana''Yi''Robin''Amal''Nori'],
   'city': ['Mexico City''Toronto''Prague''Shanghai',
          'Manchester''Cairo''Osaka'],
   'age': [41283334383137],
  'py-score': [88.079.081.080.068.061.084.0]}
row_labels = [101102103104105106107]
df = pd.DataFrame(data=data, index=row_labels)
print(df)

for (i,j) in df.iterrows():
  print(i,"\n")
  for row_data in j:
      print(row_data,"\n")
  print("\n")

outPut of code
name city age py-score 101 Xavier Mexico City 41 88.0 102 Ann Toronto 28 79.0 103 Jana Prague 33 81.0 104 Yi Shanghai 34 80.0 105 Robin Manchester 38 68.0 106 Amal Cairo 31 61.0 107 Nori Osaka 37 84.0 101 Xavier Mexico City 41 88.0 102 Ann Toronto 28 79.0 103 Jana Prague 33 81.0 104 Yi Shanghai 34 80.0 105 Robin Manchester 38 68.0 106 Amal Cairo 31 61.0 107 Nori Osaka 37 84.0




----------------------------------------------------------------------
import pandas as pd
import numpy as np
data = {
   'name': ['Xavier''Ann''Jana''Yi''Robin''Amal''Nori'],
   'city': ['Mexico City''Toronto''Prague''Shanghai',
          'Manchester''Cairo''Osaka'],
   'age': [41283334383137],
  'py-score': [88.079.081.080.068.061.084.0]}
row_labels = [101102103104105106107]
df = pd.DataFrame(data=data, index=row_labels)
print(df)

for i in df.iteritems():
  print(i,"\n")
  #for row_data in j:
  #print(j,"\n")
 # print("\n")

name city age py-score 101 Xavier Mexico City 41 88.0 102 Ann Toronto 28 79.0 103 Jana Prague 33 81.0 104 Yi Shanghai 34 80.0 105 Robin Manchester 38 68.0 106 Amal Cairo 31 61.0 107 Nori Osaka 37 84.0 ('name', 101 Xavier 102 Ann 103 Jana 104 Yi 105 Robin 106 Amal 107 Nori Name: name, dtype: object) ('city', 101 Mexico City 102 Toronto 103 Prague 104 Shanghai 105 Manchester 106 Cairo 107 Osaka Name: city, dtype: object) ('age', 101 41 102 28 103 33 104 34 105 38 106 31 107 37 Name: age, dtype: int64) ('py-score', 101 88.0 102 79.0 103 81.0 104 80.0 105 68.0 106 61.0 107 84.0
Name: py-score, dtype: float64)

---------------------------------------------------------------------------

import pandas as pd
import numpy as np
data = {
   'name': ['Xavier''Ann''Jana''Yi''Robin''Amal''Nori'],
   'city': ['Mexico City''Toronto''Prague''Shanghai',
          'Manchester''Cairo''Osaka'],
   'age': [41283334383137],
  'py-score': [88.079.081.080.068.061.084.0]}
row_labels = [101102103104105106107]
df = pd.DataFrame(data=data, index=row_labels)
print(df)

for (i,j) in df.iteritems():
  print(i,"\n")
  #for row_data in j:
  print(j,"\n")
 # print("\n")


101 Xavier Mexico City 41 88.0 102 Ann Toronto 28 79.0 103 Jana Prague 33 81.0 104 Yi Shanghai 34 80.0 105 Robin Manchester 38 68.0 106 Amal Cairo 31 61.0 107 Nori Osaka 37 84.0 name 101 Xavier 102 Ann 103 Jana 104 Yi 105 Robin 106 Amal 107 Nori Name: name, dtype: object city 101 Mexico City 102 Toronto 103 Prague 104 Shanghai 105 Manchester 106 Cairo 107 Osaka Name: city, dtype: object age 101 41 102 28 103 33 104 34 105 38 106 31 107 37 Name: age, dtype: int64 py-score 101 88.0 102 79.0 103 81.0 104 80.0 105 68.0 106 61.0 107 84.0 Name: py-score, dtype: float64



Post a Comment

0 Comments