Python 使用默认值将列添加到数据框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29517072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:41:02  来源:igfitidea点击:

Add column to dataframe with default value

pythonpandas

提问by darkpool

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.

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

采纳答案by EdChum

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

回答by Zero

Single liner works

单班轮作品

df['Name'] = 'abc'

Creates a Namecolumn and sets all rows to abcvalue

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

回答by piRSquared

You can use insertto specify where you want to new column to be. In this case, I use 0to 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

回答by Michele Piccolini

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 atafter the insertion. For example, the code above inserts the column Name as the 0-th column, i.e. it will be inserted beforethe first column, becoming the new first column. (Indexing starts from 0).

  • 分配(**kwargs)

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

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

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

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

    “禄”为您提供了索引你的列将在插入后。例如,上面的代码将列名称插入为第 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).

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