Home > OS >  Python, Isin, TypeError: only list-like objects are allowed to be passed to isin(), you passed a [st
Python, Isin, TypeError: only list-like objects are allowed to be passed to isin(), you passed a [st

Time:11-12

I'm trying to create a column status that shows if my DataFrame values are in my directory test. For example does folder O:\Stack\Over\Flow\2010 exist in the O:\Stack\Over\Flow directory.

My pl_dest DataFrame is like so:

     Folder_Name_to_create
0  O:\Stack\Over\Flow\2010
1  O:\Stack\Over\Flow\2011

Code:

import pandas as pd

pl_dest = pd.DataFrame(
    {'Folder_Name_to_create':
        [r'O:\Stack\Over\Flow\2010', r'O:\Stack\Over\Flow\2011']
    }
)
test = (r'O:\Stack\Over\Flow')

pl_dest['status']  = pl_dest['Folder_Name_to_create'].isin(test)

I receive TypeError: only list-like objects are allowed to be passed to isin(), you passed a [str].

CodePudding user response:

You can use pathlib to check if directories exist using pathlib.Path.is_dir(). If you wrap this in a basic function you can use it with df.apply()

Since your dataframe has the full path you want to check, you don't need your test at all, I don't think.

import pathlib

def my_func(test_path: str) -> bool:
    p = pathlib.Path(test_path)
    return p.is_dir()

pl_dest['status']  = pl_dest['Folder_name_to_create'].apply(my_func)

edit:

If your goal is really here to just create directories that are needed, you can do that without bothering to make the new column, and you should also use a try:...except:... rather than actually checking if they exist. You could do that like this:

for folder in pl_dest['Folder_name_to_create']:
    try:
        pathlib.Path(folder).mkdir()
    except FileExistsError:
        pass
        # or in debugging you can print(f'{folder} already exists')

CodePudding user response:

Iterate over row & check existance

from pathlib import Path
import os
import pandas as pd

status=[]

for index, row in df.iterrows():
    path = row['Folder_Name_to_create']
    path=Path(path)
    if os.path.isdir(path):
       status.append('File_exists')
    else:
       status.append('File_not_exists')

df['status']=status

Expected outputs #

Folder_Name_to_create    status 
c://:path/example        File_exists
c://:path/example2       File_not_exists

Note: Not tested...Just posting the way of execition...Try this & Let me know

  • Related