python - 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:
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)
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!
解决方案
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
推荐阅读
- sql - 使用 GROUP BY 和 COUNT 进行 SQL 选择?
- neo4j - 如何在 Neo4J 中实现日期时间数据
- wordpress - 在帖子(不是页面)中使用自定义模板
- reactjs - 如何在反应动作中编写类型脚本
- python - 字典成员键更改的Python调用函数
- flutter - 如何等待 MethodChannel 导致颤振?
- flutter - 使用 Widget 列表动态更改 Scaffold 的主体
- mysql - MySQL JSON 查找多个值
- c# - 为什么我可以在没有 INotifyPropertyChanged 的情况下更新 UI
- javascript - 如何通过键将对象转换为对象数组?