1

I'm working on script that automatically generates full file path column using df.apply() as below.

def generate_filepath(row):
    all_files = os.listdir(files_dir)
    if row['FileName'] not in all_files:
        return None
    value = os.path.join(files_dir, row['FileName'])
    return value


csv_df['FilePath'] = csv_df.apply(generate_filepath, axis=1)

I had to declare files_dir as a global variable and then use it in the function. Is there any other I can pass it as an argument along with df.apply? Kindly help me with good suggestions

Girish venkata
  • 704
  • 1
  • 7
  • 15
  • 3
    Have you tried `def generate_filepath(row, files_dir)` and then using `csv_df.apply(generate_filepath, axis=1, files_dir='whatever')` ? Any kwargs not consumed by `.apply` itself are passed to the applied function. – Jon Clements Dec 17 '18 at 18:50
  • will try that right now and let you know – Girish venkata Dec 17 '18 at 18:51
  • 1
    It also qeems a waste doing `os.listdir` for every item if it's always going to be the same filedir... – Jon Clements Dec 17 '18 at 18:52
  • @JonClements I tried and it worked and you suggest passing the os.listdir once to the function right? – Girish venkata Dec 17 '18 at 18:55
  • Ahh... coldspeed's found a duplicate for you... you might even not want to bother with apply in this case - get your listing once... and then use `np.where(...)` with suitable criteria to create your new column... – Jon Clements Dec 17 '18 at 18:58
  • @JonClements Thanks Jon will try that too! – Girish venkata Dec 17 '18 at 19:04
  • @JonClements to be fair, op's question is pretty common. There are lots of way in python to solve it. I do not know why I was downvoted for suggesting currying as the duplicate was for currying – user1462442 Dec 17 '18 at 19:06

1 Answers1

0

Why don't you curry your function?

https://www.python-course.eu/currying_in_python.php

or add another argument?

Edit: Jon Clements answer is better than mine

user1462442
  • 7,672
  • 1
  • 24
  • 27