使用默认值将列添加到dataframe

时间:2022-10-28 16:47:54

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:

现有的df:

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

New df:

新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'.

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

Im not entirely sure how to do that.

我不完全确定如何做到这一点。

4 个解决方案

#1


100  

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

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

#2


21  

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.

您可以使用insert指定新列的位置。在这种情况下,我使用0将新列放在左侧。

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

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

#3


18  

Single liner works

单衬里工作

df['Name'] = 'abc'

Creates a Name column and sets all rows to abc value

创建Name列并将所有行设置为abc值

#4


2  

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

总结其他人的建议,并增加第三种方式

You can:

您可以:

  • assign(**kwargs):

    分配(** 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)

    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(0 <= loc <= len(columns))允许您将列插入所需的位置。

    '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).

    'loc'为您提供插入后列的位置索引。例如,上面的代码将列Name作为第0列插入,即它将插入第一列之前,成为新的第一列。 (索引从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).

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

#1


100  

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

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

#2


21  

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.

您可以使用insert指定新列的位置。在这种情况下,我使用0将新列放在左侧。

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

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

#3


18  

Single liner works

单衬里工作

df['Name'] = 'abc'

Creates a Name column and sets all rows to abc value

创建Name列并将所有行设置为abc值

#4


2  

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

总结其他人的建议,并增加第三种方式

You can:

您可以:

  • assign(**kwargs):

    分配(** 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)

    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(0 <= loc <= len(columns))允许您将列插入所需的位置。

    '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).

    'loc'为您提供插入后列的位置索引。例如,上面的代码将列Name作为第0列插入,即它将插入第一列之前,成为新的第一列。 (索引从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).

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