pythonexcelpython-in-excel

How to sum delimited text in python in excel


I slowly started using python in excel. Somehow I managing the code, however this time I I have received my output data is delimited with ",". How to do it with py option excel.

Based on the below pic, sum of aa,dd,ee is 18, bb,gg is 9 and so on.. I can do it sum individual but not sure how to deal with delimited text. Any help would appreciate. Thank you.

que


Solution

  • Enter in E2 with =PY:

    df_nv = xl("A1:B10", headers=True)
    names = xl("D2:D5").iloc[:,0]
    nv = dict(zip(df_nv["Name"],df_nv["Value"]))
    
    def sum_split(a):
        return sum(map(lambda a: nv[a], a.split(",")))
    
    list(map(sum_split, names))
    

    Result


    Siddharth Rout's answer has better checks - for example xl returns dataFrame only for multi-cell inputs.