GroupBy pandas DataFrame and select most common value

后端 未结 10 1820
梦谈多话
梦谈多话 2020-11-22 07:59

I have a data frame with three string columns. I know that the only one value in the 3rd column is valid for every combination of the first two. To clean the data I have to

10条回答
  •  孤城傲影
    2020-11-22 08:39

    For agg, the lambba function gets a Series, which does not have a 'Short name' attribute.

    stats.mode returns a tuple of two arrays, so you have to take the first element of the first array in this tuple.

    With these two simple changements:

    source.groupby(['Country','City']).agg(lambda x: stats.mode(x)[0][0])
    

    returns

                             Short name
    Country City                       
    Russia  Sankt-Petersburg        Spb
    USA     New-York                 NY
    

提交回复
热议问题