Skip to content
Advertisement

How do I use pandas to open file and process each row for that file?

So I have a dataframe containing image filenames and multiple (X, Y) coordinates for each image, like this::

file x y
File1 1 2
File1 103 6
File2 6 3
File2 4 9

I want to open each file and draw the points points on it.

I want to open each file in the file column once only. (I know how to open and draw on the file, I am asking how to loop through the dataframe correctly). Then process each line for that file.

I know I can use perhaps df['file'].unique() but don’t know where to go from that.

I can probably work this out naively but is there a clever pandas way of doing it? I imagine it may have been asked but I can’t figure out how to search for it.

Advertisement

Answer

You can use groupby to group all the rows into separate dataframes for each unique value of the file columns, loop over those dataframes, and use iterrows on each dataframe to loop over each row.

for file, group_df in df.groupby('file'):
    print(f'{file}:')
    for idx, row in group_df.iterrows():
        x, y = row[['x', 'y']]
        print(f'  x={x} y={y}')

Output:

File1:
  x=1 y=2
  x=103 y=6
File2:
  x=6 y=3
  x=4 y=9
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement