在行[列]上使用 pd.notnull() 进行检查时出现值错误.替代空检查?



在我的一个熊猫专栏中,我存储了列表。

例如,[url.com, url2.com, url3.com]

以下是该列的示例打印输出:

associated_Urls
322  [http://www.hotfrog.ie/business/golf-gifts-ire...
466                    [http://en.netlog.com/A_ni_nha]
433  [https://www.moog.com.cn/literature/ICD/Moog_G...
13   [http://www.schooldays.ie/thread/Childminder-w...
438  [http://tracking.instantcheckmate.com/?a=60&c=...
308  [http://www.wayn.com/profiles/abc123, https://...
361  [https://whoswholegal.com/profiles/abcdef........

apply函数中,我使用以下方法检查这些行中的每一行是否为空:


def myfunc(row):
if pd.notnull(row['associated_Urls']):
#do something
df.apply(myfunc,axis=1)

但是我收到以下错误:

if pd.notnull(row['associated_Urls']):
ValueError: ('The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()', 'occurred at index 322')

我检查了索引 322 处的行,它不为空。列表中有一个包含 url 的列表。

检查此特定单元格是否为空的最佳方法是什么?

根据这个问题,它被修复了:pd.notnull 奇怪的空检查行为

但是我得到了错误。任何建议不胜感激。

为什么不使用:

notnull_mask = pd.notnull(df['associated_Urls'])
df.loc[notnull_mask :] = df.loc[notnull_mask, :].apply(some_func)

相关内容

最新更新