list comprehension in pandas

后端 未结 4 914
难免孤独
难免孤独 2020-12-16 07:36

I\'m giving a toy example but it will help me understand what\'s going on for something else I\'m trying to do. Let\'s say I want a new column in a dataframe \'optimal_fruit

4条回答
  •  孤街浪徒
    2020-12-16 07:54

    The reason why your new method doesn't work is because the list comprehension produces data that is longer than the number of indices in your dataframe. A quick fix for that would be something like:

    [x * y - z for x,y,z in zip(df2['apples'], df2['oranges'], df2['bananas'])]
    

提交回复
热议问题