0

Have a Pandas dataframe with the list of file names:

Files
3003.txt
3000.txt

Also having three directories in which I'd like to make search for these files:

dirnames=[
    '/Users/User/Desktop/base/reports1',
    '/Users/User/Desktop/base/reports2',
    '/Users/User/Desktop/base/reports3']

Doing like this:

for dirname in dirnames:
    for f in os.listdir(dirname):
        with open (os.path.join(dirname,f), "r", encoding="latin-1") as infile:

            if f==df.Files:

                text = infile.read() 
                print(f,dirname)

Gives the error

The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

How is it possible to do properly-rewrite the if conditions or make the for loop in another way?

Thanks!

Keithx
  • 2,994
  • 15
  • 42
  • 71
  • The issue is that `df.Files` is a series, so when you do `f==df.Files` you get a series like `True False False False False...` It's ambiguous as to what single truth value of defines that that series, without any additional information. What you really want is `(f == df.Files).any()` or `f in df.Files.tolist()` – ALollz Jul 18 '18 at 15:42

1 Answers1

1

I think you want to add Files.items(). And then compare the files to the iterator generated from that. 'Files' points to the panda Series.