Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

iterate over rows dataframe

df = pd.DataFrame([{'c1':10, 'c2':100}, {'c1':11,'c2':110}, {'c1':12,'c2':120}])
for index, row in df.iterrows():
    print(row['c1'], row['c2'])
Comment

pandas iterate over a series

>>> s = pd.Series(['A', 'B', 'C'])
>>> for index, value in s.items():
...     print(f"Index : {index}, Value : {value}")

Index : 0, Value : A
Index : 1, Value : B
Index : 2, Value : C
Comment

python - iterate with the data frame

# Option 1
for row in df.iterrows():
    print row.loc[0,'A']
    print row.A
    print row.index()

# Option 2
for i in range(len(df)) : 
  print(df.iloc[i, 0], df.iloc[i, 2]) 
Comment

pandas iterate rows

import pandas as pd
import numpy as np

df = pd.DataFrame({'c1': [10, 11, 12], 'c2': [100, 110, 120]})

for index, row in df.iterrows():
    print(row['c1'], row['c2'])
Comment

iterate over dataframe

# Method A for single column dataframe

cell = list()
for i in range(len(df)):    
    cell_value=df.iloc[i][0]  
    cell.append(cell_value)

# Method B for multiple column dataframe

 for index, row in df.iterrows():
     print(row["c1"], row["c2"])

# Method C 

columns = list(df.columns)  
for i in columns: 
    print (df[i][2])
Comment

pandas iteration

df = pd.DataFrame({'num_legs': [4, 2], 'num_wings': [0, 2]},
...                   index=['dog', 'hawk'])
>>> df
      num_legs  num_wings
dog          4          0
hawk         2          2
>>> for row in df.itertuples():
...     print(row)
...
Pandas(Index='dog', num_legs=4, num_wings=0)
Pandas(Index='hawk', num_legs=2, num_wings=2)
Comment

how to iterate through a pandas dataframe

# creating a list of dataframe columns 
columns = list(df) 
  
for i in columns: 
  
    # printing the third element of the column 
    print (df[i][2])
Comment

PREVIOUS NEXT
Code Example
Python :: reverse string python 
Python :: python time.sleep 
Python :: HOW TO CREATE A DATETIME LIST QUICK 
Python :: torch tensor equal to 
Python :: python defaultdict default value 
Python :: list and tuple difference in python 
Python :: pip ne marche pas 
Python :: store in a variable the ocntent of a file python 
Python :: To convert Date dtypes from Object to ns,UTC with Pandas 
Python :: jupyter notebook not showing all null values 
Python :: how to get the memory location of a varible in python 
Python :: what is cpython 
Python :: reduce dataframe merge 
Python :: setting python2 in the path for npm install 
Python :: how to save string json to json object python 
Python :: includes python 
Python :: plot the distribution of value_counts() python 
Python :: webex teams api attach file 
Python :: how to stop python for some time in python 
Python :: python menentukan genap ganjil 
Python :: permutation and combination in python 
Python :: how can I print all items in a tuple, separated by commas? 
Python :: django admin text box 
Python :: python bug 
Python :: get legend lables and handles from plot in matplotlib 
Python :: How to convert datetime in python 
Python :: statsmodels fitted values 
Python :: python remove multiple element from list by index 
Python :: python open file check error 
Python :: pandas.core.indexes into list 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =