How to apply a function on a Series

六月ゝ 毕业季﹏ 提交于 2019-12-01 07:12:40

Use map to perform the lookup:

In [204]:
translate = {
    'Houston Oilers': 'Tennessee Titans',
    'Tennessee Oilers': 'Tennessee Titans'
}
s.map(translate)

Out[204]:
0    Tennessee Titans
1                 NaN
2                 NaN
Name: Name, dtype: object

The reason s = s.apply(lambda x: translate.get(x, x)) fails is because the lambda here is a pandas Series and this cannot be used as a key lookup value for the error reason given as it cannot be hashed which dict keys must be.

EDIT

Actually I can't reproduce your error:

In [210]:
s.apply(lambda x: translate.get(x, x))

Out[210]:
0       Tennessee Titans
1       Tennessee Titans
2    Washington Redskins
Name: Name, dtype: object

the above works fine

Edit 1

To keep non-existing values you can call dropna and update:

In [219]:
s.update(s.map(translate).dropna())
s

Out[219]:
0       Tennessee Titans
1       Tennessee Titans
2    Washington Redskins
Name: Name, dtype: object

When you read a csv using read_csv it returns a df even if it only has a single column, if you want a series then pass param squeeze=True:

In [223]:
t="""Name
Tennessee Oilers
Tennessee Titans
Washington Redskins"""
type(pd.read_csv(io.StringIO(t), squeeze=True))

Out[223]:
pandas.core.series.Series

Edit 2

Your error occurred because you called apply on a single column df:

pd.DataFrame(s).apply(lambda x: translate.get(x, x))

So this is different to a Series where apply iterates over each value which can be hashed but here it's passing the entire Series which cannot be hashed, it would work if you did this:

In [227]:
pd.DataFrame(s).apply(lambda x: translate.get(x['Name'], x['Name']), axis=1)

Out[227]:
0       Tennessee Titans
1       Tennessee Titans
2    Washington Redskins
dtype: object

passing axis=1 performs row-wise value passing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!