0

I'd like to compare hash values in a .csv-file with the three columns image_url, code and hash. For a start i just tried to print the hash values from the rows which have code = 1.

This is my code to get the dataframe with all the rows i need

readpath = os.path.join(folder,readfile)
df = pd.read_csv(readpath)
df_code1 = df[df["code"] == 1]

but now the problems begin to start

for row in df_code1.iterrows():
    try:
        print(row['hash'])
    except Exception as e:
        print(e)

prints tuple indices must be integers or slices, not str over and over.. when i use df_code1.rows: instead i get the Errormessage 'DataFrame' object has no attribute 'rows'

I'm pretty sure i imported everything correctly.

May anyone tell me what i'm doing wrong?

lxg95
  • 553
  • 2
  • 8
  • 28

1 Answers1

2

First I think the best is avoid use DataFrame.iterrows.

Generally, iterrows should only be used in very, very specific cases.


Here is problem DataFrame.iterrows return 2 values:

index label or tuple of label
The index of the row. A tuple for a MultiIndex.

data Series
The data of the row as a Series.

So if assign output to row get tuples:

for row in df_code1.iterrows():
    print (row)

So you can assign output to 2 variables:

for i, row in df_code1.iterrows():
    print (row)

Or select second value of tuple:

for row in df_code1.iterrows():
    print (row[1])
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • nice that is working, thanks but why is df_code1.rows: telling me the 'DataFrame' object has no attribute 'rows'? I am sure i did this once with a dataFrame and that worked – lxg95 Jan 27 '21 at 10:01
  • @lxg95 - I think it is not valid pandas value. – jezrael Jan 27 '21 at 10:02