0

I have a pandas DF with two columns, one columns consists of unique filenames and the other consists of that filenames' label. I also have a numpy array of the filenames that i can use for training my model. I need to extract the labels from the DF that match the filenames i can use. I tried this:

x = []

for i in nparray:
  for j in DF['filenames']:
    if DF['filenames'][j] == nparray[i]:
      x.append(DF['label'][j])

if I do this i get a key error with the name of the first filename of the DF

2 Answers2

1

try like these

DF[DF['filenames'].isin(nparray)]

To get only label column

DF[DF['filenames'].isin(nparray)]['label']
Prince Francis
  • 2,995
  • 1
  • 14
  • 22
1
list(DF[DF['filenames'].isin(nparray)]['label'])

Will give you a list with labels in DF where it's value in filenames is in nparray

Aryerez
  • 3,417
  • 2
  • 9
  • 17