将值分配给Pandas中的多个列 [英] Assign values to multiple columns in Pandas
本文介绍了将值分配给Pandas中的多个列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遵循简单的DataFrame-df
:
I have follow simple 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)]
我遵循以下结构
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
推荐答案
类似的解决方案很简单:
Looks like solution is simple:
df['col2'], df['col3'] = zip(*[(2,3), (2,3), (2,3)])
这篇关于将值分配给Pandas中的多个列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文