我想在熊猫中创建一个新列,如果存在另一列中的路径,则返回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或整数,而不是序列
我做错了什么?
发布于 2020-03-21 11:08:53
来自@IgorRaush的评论
df['exists'] = df['file'].astype(str).map(os.path.exists)https://stackoverflow.com/questions/60780736
复制相似问题