Trying to drop rows with path that doesn’t exist…
JavaScript
x
3
1
data_docs = pd.read_csv('Documents_data.csv')
2
data_docs.drop(data_docs[os.path.exists(str(data_docs['file path']))].index, inplace=True)
3
Error:
JavaScript
1
2
1
KeyError: False
2
Advertisement
Answer
As it stands, os.path.exists
looks at the whole str
representation of the column, not element-by-element. One way is to apply
:
JavaScript
1
3
1
exists = data_docs["file path"].apply(os.path.exists)
2
data_docs = data_docs[exists]
3
If you print exists
, it will be a boolean series saying which paths exist and which do not.
JavaScript
1
3
1
exists = ~exists
2
data_docs.drop(data_docs[exists].index, inplace=True)
3
inverted exist
to drop the one with false result,
now it will drop the files that doesn’t exist.