DataFrame, apply, lambda, list comprehension

2019-07-23 11:48发布

问题:

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 up with, the data is not real..but it should work

import pandas as pd

# This is a dataframe containing the correct values
correct = pd.DataFrame([{"letters":"abc","data":1},{"letters":"ast","data":2},{"letters":"bkgf","data":3}])

# This is the dataframe containing source data
source = pd.DataFrame([{"c":"ab"},{"c":"kh"},{"c":"bkg"}])

for i,word in source["c"].iteritems():
    for j,row in correct.iterrows():       
        if word in row["letters"]:           
            source.at[i,"c"] = row["data"]    
            break

This is my attempt to a pandorable way but it fails because of the list comprehension returning a generator:

source["c"] = source["c"].apply(
lambda x: row["data"] if x in row["letters"] else x for row in 
correct.iterrows() 
)

回答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