我想在pandas数据框中创建一个新列,该列将返回True
是否存在其他列中的路径以及False
是否不存在。
我有以下示例:
> d = {'file': ["path/to/existing/file", "path/to/nonexisting/file"]}
> df = pd.DataFrame(data=d)
> df
file
0 path/to/existing/file
1 path/to/nonexisting/file
我想创建一个新列来检查数据框是否存在。结果如下
file exists
0 path/to/existing/file True
1 path/to/nonexisting/file False
我收到以下错误
def file_exists(x):
x = x.astype(str)
if os.path.exists(x):
return True
else:
return False
df["exists"] = np.where(file_exists(df["file"]), 1, 0)
TypeError:stat:路径应为字符串,字节,os.PathLike或整数,而不是Series
我究竟做错了什么?
来自@IgorRaush的评论
df['exists'] = df['file'].astype(str).map(os.path.exists)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句