Search code examples
pythonpandasglob

Using iloc keeps picking the last row of multiple data files


I have a list of 50 .csv files named data1.csv, data2.csv etc, I would like plot the first row, third column of each of these files. But first I would like to check the 50 values to ensure I'm plotting the correct thing, I have:

import glob
import pandas as pd

files = glob.glob('data*.csv')

for f in sorted(files):
    df = pd.read_csv(f)
    print(df.iloc[0,2])

The problem here is in the last line, df.iloc[0,2] prints the 3rd column of the LAST row when I want it to print the 3rd column of the FIRST row.

Essentially print(df.iloc[0,2]) prints the same values as print(df.iloc[-1,2]) and I have no idea why.

How can I check what values the first row, third column are in all of my files?


Solution

  • My mistake, pd.read.csv considers headers, but my .csv files have no headers, so we need:

    df = pd.read_csv(f,headers=None)