首页 > 解决方案 > Add array of new columns to Pandas dataframe

问题描述

How do I append a list of integers as new columns to each row in a dataframe in Pandas?

I have a dataframe which I need to append a 20 column sequence of integers as new columns. The use case is that I'm translating natural text in a cell of the row into a sequence of vectors for some NLP with Tensorflow.

But to illustrate, I create a simple data frame to append:

df = pd.DataFrame([(1, 2, 3),(11, 12, 13)])
df.head()

Which generates the output:

enter image description here

And then, for each row, I need to pass a function that takes in a particular value in the column '2' and will return an array of integers that need to be appended as columns in the the data frame - not as an array in a single cell:

def foo(x):
    return [x+1, x+2, x+3]

Ideally, to run a function like:

df[3, 4, 5] = df['2'].applyAsColumns(foo)

enter image description here

The only solution I can think of is to create the data frame with 3 blank columns [3,4,5] , and then use a for loop to iterate through the blank columns and then input them as values in the loop.

Is this the best way to do it, or is there any functions built into Pandas that would do this? I've tried checking the documentation, but haven't found anything.

Any help is appreciated!

标签: pythonpandasdataframe

解决方案


IIUC,

def foo(x):
    return pd.Series([x+1, x+2, x+3])

df = pd.DataFrame([(1, 2, 3),(11, 12, 13)])

df[[3,4,5]] = df[2].apply(foo)

df

Output:

    0   1   2   3   4   5
0   1   2   3   4   5   6
1  11  12  13  14  15  16

推荐阅读