how to make a dataframe from images python pandas

Yes, you can use pathlib to search for files like this.

Here’s an example of how to recursively search the current directory for .png files.

from pathlib import Path
image_dir_path = '.'
print(Path(image_dir_path).rglob('*.png'))

Then, you just need to reformat that into a dataframe. Here’s how to do that:

import pandas as pd
from pathlib import Path

image_dir_path = '.'
paths = [path.parts[-3:] for path in
         Path(image_dir_path).rglob('*.png')]
df = pd.DataFrame(data=paths, columns=['Automaker', 'Model', 'Images'])
print(df)

Note: you’ll probably need to change image_dir_path to the directory of images on your computer.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top