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

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

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

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

how to iterate over rows in pandas

import pandas as pd

df = pd.DataFrame({'c1': [10, 11, 12], 'c2': [100, 110, 120]})
df = df.reset_index()  # make sure indexes pair with number of rows

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

PREVIOUS NEXT
Code Example
Python :: os.execl 
Python :: delete element list python 
Python :: seaborn pairplot python 
Python :: set permissions role discord.py 
Python :: find sum numbers in a list in python 
Python :: smtplib send caleneder email 
Python :: Iterate string 2 characters at a time in python 
Python :: python loop through dictionary 
Python :: input in python 
Python :: throughput in os 
Python :: factorial of a number in python 
Python :: use django taggit in template 
Python :: bar plot bokeh 
Python :: replace list 
Python :: python pandas table save 
Python :: relative import in python 
Python :: python openpyxl cell width 
Python :: short if python 
Python :: install anaconda python 2.7 and 3.6 
Python :: get current function name in python3 
Python :: prime number using python 
Python :: hashlib sha 256 
Python :: how to find unique values in list in python 
Python :: get dict values in list python 
Python :: Display the data types of the DataFrame 
Python :: concat string columns in pandas 
Python :: insert column in a dataframe 
Python :: selenium chrome options suppress warnings python 
Python :: remove unnamed columns pandas 
Python :: plus in python 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =