How to apply a function to two columns of Pandas dataframe

前端 未结 12 1179
名媛妹妹
名媛妹妹 2020-11-22 06:17

Suppose I have a df which has columns of \'ID\', \'col_1\', \'col_2\'. And I define a function :

f = lambda x, y : my_function_expres

12条回答
  •  旧巷少年郎
    2020-11-22 06:42

    I suppose you don't want to change get_sublist function, and just want to use DataFrame's apply method to do the job. To get the result you want, I've wrote two help functions: get_sublist_list and unlist. As the function name suggest, first get the list of sublist, second extract that sublist from that list. Finally, We need to call apply function to apply those two functions to the df[['col_1','col_2']] DataFrame subsequently.

    import pandas as pd
    
    df = pd.DataFrame({'ID':['1','2','3'], 'col_1': [0,2,3], 'col_2':[1,4,5]})
    mylist = ['a','b','c','d','e','f']
    
    def get_sublist(sta,end):
        return mylist[sta:end+1]
    
    def get_sublist_list(cols):
        return [get_sublist(cols[0],cols[1])]
    
    def unlist(list_of_lists):
        return list_of_lists[0]
    
    df['col_3'] = df[['col_1','col_2']].apply(get_sublist_list,axis=1).apply(unlist)
    
    df
    

    If you don't use [] to enclose the get_sublist function, then the get_sublist_list function will return a plain list, it'll raise ValueError: could not broadcast input array from shape (3) into shape (2), as @Ted Petrou had mentioned.

提交回复
热议问题