将列添加到具有恒定值的数据框

问题:将列添加到具有恒定值的数据框

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

现有的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

我知道如何追加现有的series / dataframe列。但这是另一种情况,因为我所需要的只是添加“名称”列,并将每一行设置为相同的值,在本例中为“ abc”。

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

Existing df:

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

New df:

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

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’.


回答 0

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

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

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

回答 1

您可以使用insert指定要在何处添加新列。在这种情况下,我通常0将新列放在左侧。

df.insert(0, 'Name', 'abc')

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

You can use insert to specify where you want to new column to be. In this case, I use 0 to place the new column at the left.

df.insert(0, 'Name', 'abc')

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

回答 2

单班轮工程

df['Name'] = 'abc'

创建一Name列并将所有行设置为abcvalue

Single liner works

df['Name'] = 'abc'

Creates a Name column and sets all rows to abc value


回答 3

总结其他人的建议,并添加第三种方法

您可以:

  • 分配(** kwargs)

    df.assign(Name='abc')
  • 访问新的列系列(将被创建)并进行设置:

    df['Name'] = 'abc'
  • 插入(位置,列,值,allow_duplicates = False)

    df.insert(0, 'Name', 'abc')

    参数loc(0 <= loc <= len(columns))允许您在所需的位置插入列。

    “禄”为您提供了索引你的列将在插入后。例如,上面的代码将Name列插入第0列,即它将插入到第一列之前,成为新的第一列。(索引从0开始)。

所有这些方法都允许您从系列中添加新列(只需将上面的’abc’默认参数替换为系列)。

Summing up what the others have suggested, and adding a third way

You can:

  • assign(**kwargs):

    df.assign(Name='abc')
    
  • access the new column series (it will be created) and set it:

    df['Name'] = 'abc'
    
  • insert(loc, column, value, allow_duplicates=False)

    df.insert(0, 'Name', 'abc')
    

    where the argument loc ( 0 <= loc <= len(columns) ) allows you to insert the column where you want.

    ‘loc’ gives you the index that your column will be at after the insertion. For example, the code above inserts the column Name as the 0-th column, i.e. it will be inserted before the first column, becoming the new first column. (Indexing starts from 0).

All these methods allow you to add a new column from a Series as well (just substitute the ‘abc’ default argument above with the series).