TypeError:字符串索引必须是使用pandas应用于lambda的整数 [英] TypeError: string indices must be integers using pandas apply with lambda

查看:99
本文介绍了TypeError:字符串索引必须是使用pandas应用于lambda的整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,一列是URL,另一列是名称.我只是想添加第三列以获取URL,并创建一个HTML链接.

I have a dataframe, one column is a URL, the other is a name. I'm simply trying to add a third column that takes the URL, and creates an HTML link.

newsSource具有链接名称,列url具有URL.对于数据框中的每一行,我要创建一个具有以下内容的列:

The column newsSource has the Link name, and url has the URL. For each row in the dataframe, I want to create a column that has:

<a href="[the url]">[newsSource name]</a>

尝试以下操作会引发错误

Trying the below throws the error

文件"C:\ Users \ AwesomeMan \ Documents \ Python \ MISC \ News Alerts \ simple_news.py",第254行,在 df ['sourceURL'] = df ['url'].apply(lambda x:'{1}'.format(x,x [0] ['newsSource']))
TypeError:字符串索引必须为整数

File "C:\Users\AwesomeMan\Documents\Python\MISC\News Alerts\simple_news.py", line 254, in df['sourceURL'] = df['url'].apply(lambda x: '{1}'.format(x, x[0]['newsSource']))
TypeError: string indices must be integers

df['sourceURL'] = df['url'].apply(lambda x: '<a href="{0}">{1}</a>'.format(x, x['source']))

但是我以前用过x[colName]吗?下面的代码行很好,它只是创建了源名称的列:

But I've used x[colName] before? The below line works fine, it simply creates a column of the source's name:

df['newsSource'] = df['source'].apply(lambda x: x['name'])

为什么突然(对我突然")说我无法访问索引?

Why suddenly ("suddenly" to me) is it saying I can't access the indices?

推荐答案

要逐行访问多个系列,您需要 pd.DataFrame.apply axis=1:

To access multiple series by row, you need pd.DataFrame.apply along axis=1:

def return_link(x):
    return '<a href="{0}">{1}</a>'.format(x['url'], x['source'])

df['sourceURL'] = df.apply(return_link, axis=1)

请注意,以这种方式传递整个系列会产生开销; pd.DataFrame.apply只是一个薄薄的,低效的循环.

Note there is an overhead associated with passing an entire series in this way; pd.DataFrame.apply is just a thinly veiled, inefficient loop.

您可能会发现列表理解更有效:

You may find a list comprehension more efficient:

df['sourceURL'] = ['<a href="{0}">{1}</a>'.format(i, j) \
                   for i, j in zip(df['url'], df['source'])]

这是一个工作示例:

df = pd.DataFrame([['BBC', 'http://www.bbc.o.uk']],
                  columns=['source', 'url'])

def return_link(x):
    return '<a href="{0}">{1}</a>'.format(x['url'], x['source'])

df['sourceURL'] = df.apply(return_link, axis=1)

print(df)

  source                  url                              sourceURL
0    BBC  http://www.bbc.o.uk  <a href="http://www.bbc.o.uk">BBC</a>

这篇关于TypeError:字符串索引必须是使用pandas应用于lambda的整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆