list comprehension in pandas

后端 未结 4 899
难免孤独
难免孤独 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:49

    You can get all the values of the row as a list using the np.array() function inside your list of comprehension.

    The following code solves your problem:

    df2['optimal_fruit'] = [x[0] * x[1] - x[2] for x in np.array(df2)]
    

    It is going to avoid the need of typing each column name in your list of comprehension.

提交回复
热议问题