I have a folder which comprises of different images like dell_01.png, hp_01.png, toshiba_01.png and would like to create a dataframe from it which would look like this:
If the file starts with hp, it should be assigned to class 1. If it starts with toshiba, it should be assigned to class 1. If it starts with dell, it should be assigned to class 2 as seen in the below expected dataframe output.
filename class
hp_01.png 0
toshiba_01.png 1
dell_01.png 2
CodePudding user response:
Break the problem up:
I have a folder... different images
So you need to get the filenames from the folder. User pathlib.Path.glob
:
from pathlib import Path
for fn in Path("/path/to/folder/").glob("*.png"):
...
if the file starts with
hp
... class 1,toshiba
... class 2 so you have a condition.
if fn.stem.startswith("hp"):
class = 1
elif ...
Now you can solve the two parts individually.
in a dataframe
Use the two constructs above to make a dictionary for every file. Then make a dataframe from that dict. Your code will look something like this:
files = []
for fn in Path("/path/to/pngs").glob("*.png"):
if fn.stem.startwith("hp"):
class = 0
...
files.append({"filename": fn.name, "class": class})
(Yes, there are more direct routes to getting this into a dataframe, but I was trying to make what was going on clearer.)
Does that help? I've deliberately avoided just writing the answer for you, but tried to get just close enough you can fill in the rest.