How to apply a function on a Series

后端 未结 1 1328
广开言路
广开言路 2021-01-13 14:34

Given a Series s:

                  Name
0     Tennessee Oilers
1     Tennessee Titans
2  Washington Redskins

I would like a a

相关标签:
1条回答
  • 2021-01-13 14:58

    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

    0 讨论(0)
提交回复
热议问题