【发布时间】:2022-11-12 13:46:43
【问题描述】:
我正在尝试创建一个列status,以显示我的DataFrame 值是否在我的目录test 中。例如文件夹O:\Stack\Over\Flow\2010 是否存在于O:\Stack\Over\Flow 目录中。
我的pl_dest DataFrame 是这样的:
Folder_Name_to_create
0 O:\Stack\Over\Flow\2010
1 O:\Stack\Over\Flow\2011
代码:
import pandas as pd
pl_dest = pd.DataFrame(
{'Folder_Name_to_create':
[r'O:\Stack\Over\Flow\2010', r'O:\Stack\Over\Flow\2011']
}
)
test = (r'O:\Stack\Over\Flow')
pl_dest['status'] = pl_dest['Folder_Name_to_create'].isin(test)
我收到TypeError: only list-like objects are allowed to be passed to isin(), you passed a [str]。
【问题讨论】:
-
isin想要一个列表,而你给了它一个字符串。你试过给它一个清单吗? (另外,请将数据/代码作为文本发布,not as images。) -
如果你想让
test成为一个元组,它需要一个尾随逗号test = (r'O:\Stack\Over\Flow',),或者如果你想创建一个列表使用方括号test = [r'O:\Stack\Over\Flow'] -
好吧,我列出了
test。代码现在可以工作了!虽然我的status列仍然显示错误!我认为这是因为我需要test来读取我的文件目录,而不仅仅是使用O:\Stack\Over\Flow。我在想isin可能不适合检查目录O:\Stack\Over\Flow的pl_dest值。