Skip to content Skip to sidebar Skip to footer

Dataframe, Apply, Lambda, List Comprehension

I'm trying to do a bit of cleanse to some data sets, I can accomplish the task with some for loops but I wanted a more pythonic/pandorable way to do this. This is the code I came u

Solution 1:

Here's one solution using pd.Series.apply with next and a generator expression:

def update_value(x):
    return next((k for k, v in correct.set_index('data')['letters'].items() if x in v), x)

source['c'] = source['c'].apply(update_value)

print(source)

    c
0   1
1  kh
2   3

Post a Comment for "Dataframe, Apply, Lambda, List Comprehension"