1

I do want to read some .csv files and print the minimum and maximum value of the 5th column. I also want to print in which file I can find the max or min.

My code is right now:

import pandas, glob
import numpy as np

path = "/home/path/to/log/"

fn = glob.glob(path + "*.csv") 
list_of_dfs = [pandas.read_csv(filename, header=None) for filename in fn]
k = len(list_of_dfs)
b = np.zeros((k, 1))
cnt = 0 
for i in list_of_dfs:
    b[cnt,0] = np.min(i[4])
    cnt = cnt + 1
print(np.min(b[:,0]))
print(np.argmin(b[:,0]))

My .csv Files are named like:

0.csv, 1.csv ... 10.csv ...2463.csv

After seeing that my argmin doesn't show the correct file I realised that fn isn't sorted. I found this Sort filenames in directory in ascending order, where the solution was:

fn.sort(key=lambda f: int(filter(str.isdigit, f)))

But with this line I get the following error:

TypeError: int() argument must be a string, a bytes-like object or a number, not 'filter'

Any Suggestions?

ChrizZlyBear
  • 121
  • 2
  • 11

1 Answers1

5

I think need sorted with convert file names to integers:

fn = ['0.csv', '1.csv', '2463.csv', '10.csv']

fn = sorted(fn, key=lambda f: int(f.split('.')[0]))
print (fn)
['0.csv', '1.csv', '10.csv', '2463.csv']

If there are full paths:

print (fn)
['files\\1.csv', 'files\\10.csv', 'files\\2.csv']

fn = sorted(fn, key=lambda f: int(os.path.basename(f).split('.')[0]))
print (fn)
['files\\1.csv', 'files\\2.csv', 'files\\10.csv']
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252