简体   繁体   中英

Unable to apply function to selected columns in pandas data frame

I have a dataframe like below:

df = pd.DataFrame(data=[[[1,0],[1,1,1,1,1,1,1,1,1,1],[1,0,0,1,1,0,0,1,1],[2,3],[1,1,1,2,2,2,3,3,3,1,1,1]]], columns=['A', 'B', 'C', 'D', 'E'])

在此处输入图片说明

I want to apply the below functions to selected columns in the data frame:

def item_exclude_all(list1):
    from itertools import groupby
    out = [i[0] for i in groupby(list1)]
    return out

def fl(x):
    new = []
    for i in x:
        new.append([i[0], i[-1]])
    return new

I did the following operation by applying "fl" function, which worked without error:

df.apply(lambda x : fl(x) if x.name in ['C', 'E'] else x)

在此处输入图片说明

But when i'm trying to apply the other function "item_exclude_all", function is not getting applied to the selected columns.

df.apply(lambda x : item_exclude_all(x) if x.name in ['C', 'E'] else x)

I'm trying to understand what could be the issue here in the above code.

在此处输入图片说明

You have to use .apply() inside the first .apply() . This is because the item_exclude_all() function should be applied over each row, not over the entire Serie.

df.apply(lambda x : x.apply(item_exclude_all) if x.name in ['C', 'E'] else x)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM