Pandas Python:如何从列表中创建多列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51495800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:49:57  来源:igfitidea点击:

Pandas Python : how to create multiple columns from a list

pythonpandasdataframe

提问by Vincent

I have a list with columns to create :

我有一个包含要创建的列的列表:

new_cols = ['new_1', 'new_2', 'new_3']

I want to create these columns in a dataframe and fill them with zero :

我想在数据框中创建这些列并用零填充它们:

df[new_cols] = 0

Get error :

获取错误:

"['new_1', 'new_2', 'new_3'] not in index"

which is true but unfortunate as I want to create them...

这是真的,但很不幸,因为我想创建它们......

EDIT : This is a duplicate of this question : Pandas: Add multiple empty columns to DataFramehowever I keep this one too because the accepted answer here was the simple solution I was looking for, and it was not he accepted answer out there

编辑:这是这个问题的重复:Pandas: Add multiple empty columns to DataFrame但是我也保留了这个,因为这里接受的答案是我正在寻找的简单解决方案,而他并没有接受那里的答案

采纳答案by Fabian Ying

You need to add the columns one by one.

您需要将列一一添加。

for col in new_cols:
    df[col] = 0

Also see the answers in herefor other methods.

另请参阅此处的答案以了解其他方法。

回答by Rabin Lama Dong

import pandas as pd

new_cols = ['new_1', 'new_2', 'new_3']
df = pd.DataFrame.from_records([(0, 0, 0)], columns=new_cols)

Is this what you're looking for ?

这是您要找的吗?

回答by Praveen KR

We can use the Apply function to loop through the columns in the dataframe and assigning each of the element to a new field for instance for a list in a dataframe with a list named keys

我们可以使用 Apply 函数循环遍历数据帧中的列,并将每个元素分配给一个新字段,例如数据帧中的列表具有名为键的列表

[10,20,30]

In your case since its all 0 we can directly assign them as 0 instead of looping through. But if we have values we can populate them as below ...

在你的情况下,因为它都是 0,我们可以直接将它们分配为 0 而不是循环。但是如果我们有值,我们可以按如下方式填充它们......

df['new_01']=df['keys'].apply(lambda x: x[0])
df['new_02']=df['keys'].apply(lambda x: x[1])
df['new_03']=df['keys'].apply(lambda x: x[2])

回答by Nordle

Try looping through the column names before creating the column:

在创建列之前尝试遍历列名:

for col in new_cols:
    df[col] = 0

回答by jezrael

Use assignby dictionary:

assign按字典使用:

df = pd.DataFrame({
    'A': ['a','a','a','a','b','b','b','c','d'],
    'B': list(range(9))
})
print (df)
0  a  0
1  a  1
2  a  2
3  a  3
4  b  4
5  b  5
6  b  6
7  c  7
8  d  8

new_cols = ['new_1', 'new_2', 'new_3']
df = df.assign(**dict.fromkeys(new_cols, 0))
print (df)
   A  B  new_1  new_2  new_3
0  a  0      0      0      0
1  a  1      0      0      0
2  a  2      0      0      0
3  a  3      0      0      0
4  b  4      0      0      0
5  b  5      0      0      0
6  b  6      0      0      0
7  c  7      0      0      0
8  d  8      0      0      0

回答by FLab

You can use assign:

您可以使用分配

new_cols = ['new_1', 'new_2', 'new_3']
values = [0, 0, 0]   # could be anything, also pd.Series

df = df.assign(**dict(zip(new_cols, values)