Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Iterate over df

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

 #With iterrows method 

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

 #With itertuples method

 for row in df.itertuples(index=True, name='Pandas'):
     print(row.c1, row.c2)
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

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

PREVIOUS NEXT
Code Example
Python :: add conda env to jupyter 
Python :: clear multiprocessing queue python 
Python :: DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning. 
Python :: choice random word in python from a text file 
Python :: use selenium without opening browser 
Python :: pyqt5 set window icon 
Python :: django create empty migration 
Python :: python write to json with indent 
Python :: pillow python crop 
Python :: python play sound 
Python :: pygame get mouse position 
Python :: python all possible combinations of multiple lists 
Python :: python time delay 
Python :: random date python 
Python :: python - convert index to a column 
Python :: split string form url last slash 
Python :: flask secret key generator 
Python :: get directory of file python 
Python :: save dictionary python 
Python :: timestamp to date python 
Python :: tensorflow load h5 model 
Python :: python get newest file in directory 
Python :: check if string url python 
Python :: frequency count of values in pandas dataframe 
Python :: image to text python 
Python :: pytest ignore warnings 
Python :: how to create dynamic variable names in python 
Python :: autoclicker in python 
Python :: How to convert an integer number into words in python? 
Python :: install flake8 python 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =