我在pandas DataFrame中有一个这样的数据框,其中A
是列名:
results.head()
A
when you are away
when I was away
when they are away
我想添加一个新的B列,如下所示:
A B
when you are away you
when I was away I
when they are away they
我尝试使用此代码,但没有用:
results.assign(B = you, I, they)
我是熊猫数据框的新手,非常感谢您的帮助。
答案 0 :(得分:1)
尝试一下:
B_list = ['you','I','they']
results['B'] = B_list
OR
results = results.assign(B=['you', 'I', 'they'])
答案 1 :(得分:0)
如果您有兴趣将列放置在特定位置(索引),请使用insert方法:
results.insert(index, "ColName" , new_set)
否则,Mayank的答案就更简单了。