pythonlocate

Locating specific file types in a column


One column in report is titled 'Filename' and there are several possible options 'xxxx.pdf', 'xxxx.Empty'.

How do I get Python to locate only files that are '.Empty' for example?

I've been trying the following code which doesn't produce an error but also brings up no results when it should

df3.loc[df3['Filename'] == '.Empty']

Thanks in advance


Solution

  • Suppose you have a dataframe like this

    df = pd.DataFrame([['a.Empty'], ['b.pdf']], columns=['file'])
    
        file
    0   a.Empty
    1   b.pdf
    

    To get all the files that are Empty use str.contains

    df[df['file'].str.contains('.Empty')]
    
        file
    0   a.Empty
    

    If you want all files except empty then

    df[~df['file'].str.contains('.Empty')]
    
        file
    1   b.pdf