DataFrame, apply, lambda, list comprehension

≡放荡痞女 提交于 2019-12-12 17:22:50

问题


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


来源:https://stackoverflow.com/questions/51180102/dataframe-apply-lambda-list-comprehension

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