使用默认值将列添加到数据框 [英] Add column to dataframe with default value

查看:56
本文介绍了使用默认值将列添加到数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的数据框,我需要添加一个额外的列,每行将包含相同的值.

I have an existing dataframe which I need to add an additional column to which will contain the same value for every row.

现有df:

Date, Open, High, Low, Close
01-01-2015, 565, 600, 400, 450

新df:

Name, Date, Open, High, Low, Close
abc, 01-01-2015, 565, 600, 400, 450

我知道如何附加现有的系列/数据框列.但这是另一种情况,因为我所需要的只是添加名称"列,并将每一行设置为相同的值,在本例中为"abc".

I know how to append an existing series / dataframe column. But this is a different situation, because all I need is to add the 'Name' column and set every row to the same value, in this case 'abc'.

我不太确定该怎么做.

推荐答案

df['Name']='abc'将添加新列并将所有行设置为该值:

df['Name']='abc' will add the new column and set all rows to that value:

In [79]:

df
Out[79]:
         Date, Open, High,  Low,  Close
0  01-01-2015,  565,  600,  400,    450
In [80]:

df['Name'] = 'abc'
df
Out[80]:
         Date, Open, High,  Low,  Close Name
0  01-01-2015,  565,  600,  400,    450  abc

这篇关于使用默认值将列添加到数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆