【问题标题】:Apply function doesnt work, the type error is TypeError: argument of type 'NoneType' is not iterable应用函数不起作用,类型错误是 TypeError: 'NoneType' 类型的参数不可迭代
【发布时间】:2020-06-28 21:01:30
【问题描述】:

我不知道为什么它不起作用,我想使用 apply 在数据帧中使用该函数,但类型错误是 TypeError: 'NoneType' 类型的参数不可迭代

df[column1]  = ['https://www.some_text.com/ar?cc+cc&q={keyword}',
'https://www.some_text.com/ar?fsd+fsd&q={keyword}',
'https://www.some_text.com/ar?xc+sdc&q={keyword}']
url_s = 'some_text'
url_f = 'some_text_2'

def url_name(x):
        if url_s in x:
        a = x.replace(url_s,url_f)
        return a

df['column1'] = df['column1'].apply(url_name)

【问题讨论】:

  • 欢迎堆栈溢出!请更新以包含minimal reproducible example,包括df['column1'] 的样本。该错误表明您尝试apply 的变量是None

标签: python


【解决方案1】:

您在 IF 循环中的缩进存在一个小问题,这可能会导致该错误。其他一切似乎都有效。

url_s = 'some_text'
url_f = 'some_text_2'

def url_name(x):

        if url_s in x:
            a = x.replace(url_s,url_f)
        return a    


d = {'column1': ['https://www.some_text.com/ar?cc+cc&q={keyword}',
'https://www.some_text.com/ar?fsd+fsd&q={keyword}',
'https://www.some_text.com/ar?xc+sdc&q={keyword}']}
df = pandas.DataFrame(data=d)
df['column1'] = df['column1'].apply(url_name)

这就是结果。

df
Out[22]: 
                                             column1
0   https://www.some_text_2.com/ar?cc+cc&q={keyword}
1  https://www.some_text_2.com/ar?fsd+fsd&q={keyw...
2  https://www.some_text_2.com/ar?xc+sdc&q={keyword}

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2011-10-04
    • 1970-01-01
    • 1970-01-01
    • 2019-04-15
    • 2012-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多