为 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
Assign values to multiple columns in Pandas
提问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
alternatively assign
can be used
或者assign
可以使用
df.assign(col2 = 2, col3= 3)
df.assign(col2 = 2, col3= 3)
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.assign.html
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.assign.html
回答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']] = 0
doesn'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)