Combine columns in a Pandas DataFrame to a column of lists in a DataFrame

后端 未结 1 904
悲哀的现实
悲哀的现实 2020-12-15 08:12

Consider the following DataFrame.

n  v1 v2 v3 v4 v5
0   1  2  3  4  5
1   1  2  3  4  5
2   1  2  3  4  5

For each row, I want

相关标签:
1条回答
  • 2020-12-15 08:56

    You can do it in one line like this:

    >>> df['v6'] = df[['v2', 'v3', 'v4']].mul(df['v5'], axis=0).values.tolist()
    >>> df
       v1  v2  v3  v4  v5            v6
    0   1   2   3   4   5  [10, 15, 20]
    1   1   2   3   4   5  [10, 15, 20]
    2   1   2   3   4   5  [10, 15, 20]
    

    This does the relevant multiplication of columns, puts the v2, v3 and v4 values out to a list of lists (row by row) and creates the new column v6.

    0 讨论(0)
提交回复
热议问题