Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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

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 :: python print user input 
Python :: how to check if item is file in python or not 
Python :: append file to list python 
Python :: what is cleaned data in django 
Python :: python extract value from a list of dictionaries 
Python :: request.body django 
Python :: how to print in pyhton 
Python :: how to randomize order of a list python 
Python :: pygame holding a button down 
Python :: display video in jupyter notebook 
Python :: initialize an array in python 
Python :: pandas row where value in list 
Python :: how to output random letters in python 
Python :: Add new column based on condition on some other column in pandas. 
Python :: install lz4 python 3 
Python :: flask render_template 
Python :: python mysqldb 
Python :: groupby year datetime pandas 
Python :: python from timestamp to string 
Python :: make sure text is on page selenium python 
Python :: python textbox 
Python :: charcodeat python 
Python :: write text in list to text file python 
Python :: create virtual env 
Python :: calculate nth prime number python 
Python :: read json from api python 
Python :: Concatenate Item in list to strings 
Python :: localize timezone python 
Python :: python sort dict by key 
Python :: django update model 
ADD CONTENT
Topic
Content
Source link
Name
2+9 =