Python Forum
loop through python pandas data frame
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
loop through python pandas data frame
#1
Hello,

I tried to loop through pandas data frame.
However I receive an error message.

with a code like this:

one error is unexpected EOF while parsing,
another is row is not define.
Please advise or does someone have a working example of how to loop through a pandas data frame ?

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

for index, row in df.iterrows():
    print(row['c1'], row['c2'])
Reply
#2
The creation of the dataframe can be simplified but everything works fine for me
import pandas as pd

inp = [{'c1':10, 'c2':100}, {'c1':11,'c2':110}, {'c1':12,'c2':120}]
df = pd.DataFrame(inp)
print(df)
Output:
c1 c2 0 10 100 1 11 110 2 12 120

for index, row in df.iterrows():
    #print(index)
    #print(row)
    print(row['c1'], row['c2'])
    print()
Output:
10 100 11 110 12 120
Reply
#3
I am getting this exact error message running the second part of your code.
I don't know why.


Quote:for index, row in df.iterrows():
File "<ipython-input-5-d488e4dd19c4>", line 1
for index, row in df.iterrows():
^
SyntaxError: unexpected EOF while parsing
Reply
#4
That´s interesting.
Maybe there is some unknown (invisible) character in front of this code?
Please use a new cell (i assume you are using Jupyter notebook)
and do not copy paste but type code into it.
Reply
#5
(Sep-01-2019, 06:19 PM)ThomasL Wrote: That´s interesting. Maybe there is some unknown (invisible) character in front of this code? Please use a new cell (i assume you are using Jupyter notebook) and do not copy paste but type code into it.

I did retype it and use instead of c1, c2, d1, d2.
I am not using Jupyter, but Spyder.

(Sep-01-2019, 06:19 PM)ThomasL Wrote: That´s interesting. Maybe there is some unknown (invisible) character in front of this code? Please use a new cell (i assume you are using Jupyter notebook) and do not copy paste but type code into it.

I found out what happen.
I was running the code line by line is why I got those message.
When I run them as chunks it works. Thanks !
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pandas: how to split one row of data to multiple rows and columns in Python GerardMoussendo 4 418 Feb-22-2021, 06:51 PM
Last Post: eddywinch82
  Yahoo_fin, Pandas: how to convert data table structure in csv file detlefschmitt 14 965 Feb-15-2021, 12:58 PM
Last Post: detlefschmitt
  get year information from a timestamp data frame asli 1 212 Jan-08-2021, 09:11 PM
Last Post: Larz60+
  Pandas Extract data from two dataframe nio74maz 1 260 Dec-26-2020, 09:52 PM
Last Post: nio74maz
  update column in one data frame with value of column from another data frame flexer 0 245 Dec-04-2020, 03:29 PM
Last Post: flexer
  Pandas, How to trigger parallel loop Mekala 4 558 Oct-29-2020, 12:58 PM
Last Post: Mekala
  Iterating Through Data Frame Rows JoeDainton123 1 605 Aug-06-2020, 05:54 AM
Last Post: scidam
  Extracting Rows From Data Frame and Understanding The Code JoeDainton123 0 400 Aug-03-2020, 04:08 PM
Last Post: JoeDainton123
  data frame excel cell calulation buunaanaa 1 634 Jul-04-2020, 06:00 PM
Last Post: buunaanaa
  Knowing the index of a data frame Ivannovix 1 606 May-01-2020, 02:51 PM
Last Post: klllmmm

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020