为 Pandas 中的多列赋值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34074663/
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 00:19:10  来源:igfitidea点击:

Assign values to multiple columns in Pandas

pythonpandaspython-2.7dataframeseries

提问by SpanishBoy

I have follow simple DataFrame - df:

我遵循简单的 DataFrame - df

   0
0  1
1  2
2  3

Once I try to create a new columns and assign some values for them, as example below:

一旦我尝试创建一个新列并为它们分配一些值,如下例所示:

df['col2', 'col3'] = [(2,3), (2,3), (2,3)]
df['col2', 'col3'] = [(2,3), (2,3), (2,3)]

I got following structure

我得到了以下结构

   0 (col2, col3)
0  1    (2, 3)
1  2    (2, 3)
2  3    (2, 3)

However, I am looking a way to get as here:

但是,我正在寻找一种方法来达到这里:

   0 col2, col3
0  1    2,   3
1  2    2,   3
2  3    2,   3

回答by SpanishBoy

Looks like solution is simple:

看起来解决方案很简单:

df['col2'], df['col3'] = zip(*[(2,3), (2,3), (2,3)])

回答by jpp

There is a convenient solution to joining multiple series to a dataframe via a list of tuples. You can construct a dataframe from your list of tuples beforeassignment:

通过元组列表将多个系列连接到数据帧有一个方便的解决方案。您可以分配之前从元组列表中构造一个数据框:

df = pd.DataFrame({0: [1, 2, 3]})
df[['col2', 'col3']] = pd.DataFrame([(2,3), (2,3), (2,3)])

print(df)

   0  col2  col3
0  1     2     3
1  2     2     3
2  3     2     3

This is convenient, for example, when you wish to join an arbitrary number of series.

这很方便,例如,当您希望加入任意数量的系列时。

回答by as - if

回答by elPastor

I ran across this issue when trying to apply multiple scalar values to multiple new columns and couldn't find a better way. If I'm missing something blatantly obvious, let me know, but df[['b','c']] = 0doesn't work. but here's the simplified code:

我在尝试将多个标量值应用于多个新列时遇到了这个问题,但找不到更好的方法。如果我遗漏了一些明显明显的内容,请告诉我,但df[['b','c']] = 0不起作用。但这是简化的代码:

# Create the "current" dataframe
df = pd.DataFrame({'a':[1,2]})

# List of columns I want to add
col_list = ['b','c']

# Quickly create key : scalar value dictionary
scalar_dict = { c : 0 for c in col_list }

# Create the dataframe for those columns - key here is setting the index = df.index
df[col_list] = pd.DataFrame(scalar_dict, index = df.index)

Or, what appears to be slightly faster is to use .assign():

或者,看起来稍微快一点的是使用.assign()

df = df.assign(**scalar_dict)