Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 to add the values of v2, v3, v4 to a list and multiply the values in the list with v5 and put the result into a new column v6 such that I end up with a DataFrame like this:

n  v1  v6
0   1  [10, 15, 20]
1   1  [10, 15, 20]
2   1  [10, 15, 20]

How can I achieve this in Pandas?

like image 466
kasperhj Avatar asked Nov 26 '14 09:11

kasperhj


People also ask

How do I merge columns in pandas DataFrame?

By use + operator simply you can combine/merge two or multiple text/string columns in pandas DataFrame. Note that when you apply + operator on numeric columns it actually does addition instead of concatenation.

How do I combine columns of different data frames?

The concat() function in pandas is used to append either columns or rows from one DataFrame to another. The concat() function does all the heavy lifting of performing concatenation operations along an axis while performing optional set logic (union or intersection) of the indexes (if any) on the other axes.


1 Answers

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.

like image 109
Alex Riley Avatar answered Oct 15 '22 21:10

Alex Riley