Pandas how to apply multiple functions to dataframe

前端 未结 4 2190
粉色の甜心
粉色の甜心 2020-12-03 13:39

Is there a way to apply a list of functions to each column in a DataFrame like the DataFrameGroupBy.agg function does? I found an ugly way to do it like this:



        
4条回答
  •  隐瞒了意图╮
    2020-12-03 14:30

    I am using pandas to analyze Chilean legislation drafts. In my dataframe, the list of authors are stored as a string. The answer above did not work for me (using pandas 0.20.3). So I used my own logic and came up with this:

    df.authors.apply(eval).apply(len).sum()
    

    Concatenated applies! A pipeline!! The first apply transforms

    "['Barros Montero: Ramón', 'Bellolio Avaria: Jaime', 'Gahona Salazar: Sergio']"
    

    into the obvious list, the second apply counts the number of lawmakers involved in the project. I want the size of every pair (lawmaker, project number) (so I can presize an array where I will study which parties work on what).

    Interestingly, this works! Even more interestingly, that last call fails if one gets too ambitious and does this instead:

    df.autores.apply(eval).apply(len).apply(sum)
    

    with an error:

    TypeError: 'int' object is not iterable
    

    coming from deep within /site-packages/pandas/core/series.py in apply

提交回复
热议问题