382

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

cs95
  • 379,657
  • 97
  • 704
  • 746
darkpool
  • 13,822
  • 16
  • 54
  • 89

10 Answers10

534

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
EdChum
  • 376,765
  • 198
  • 813
  • 562
  • 2
    What when i would like to insert new column with each constant value equal to some list? For example i would like to insert `Trajectory` column with each row value equal to `[0,0,0]`? It doesn't work your way `df['Trajectory'] = [0,0,0]` because pandas understands `list` as whole column. – s.paszko Jul 21 '22 at 11:32
115

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
piRSquared
  • 285,575
  • 57
  • 475
  • 624
90

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

You can:

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

Jolta
  • 2,620
  • 1
  • 29
  • 42
Michele Piccolini
  • 2,634
  • 16
  • 29
65

Single liner works

df['Name'] = 'abc'

Creates a Name column and sets all rows to abc value

Zero
  • 74,117
  • 18
  • 147
  • 154
31

I want to draw more attention to a portion of @michele-piccolini's answer.

I strongly believe that .assign is the best solution here. In the real world, these operations are not in isolation, but in a chain of operations. And if you want to support a chain of operations, you should probably use the .assign method.

Here is an example using snowfall data at a ski resort (but the same principles would apply to say ... financial data).

This code reads like a recipe of steps. Both assignment (with =) and .insert make this much harder:

raw = pd.read_csv('https://github.com/mattharrison/datasets/raw/master/data/alta-noaa-1980-2019.csv',
                  parse_dates=['DATE'])
def clean_alta(df):
    return (df
            .loc[:, ['STATION', 'NAME', 'LATITUDE', 'LONGITUDE', 'ELEVATION', 'DATE', 
                     'PRCP', 'SNOW', 'SNWD', 'TMAX', 'TMIN', 'TOBS']]
            .groupby(pd.Grouper(key='DATE', freq='W'))
            .agg({'PRCP': 'sum', 'TMAX': 'max', 'TMIN': 'min', 'SNOW': 'sum', 'SNWD': 'mean'})
            .assign(LOCATION='Alta', 
                    T_RANGE=lambda w_df: w_df.TMAX-w_df.TMIN)
    )

clean_alta(raw)

Notice the line .assign(LOCATION='Alta', that creates a column with a single value in the middle of the rest of the operations.

Matt Harrison
  • 1,225
  • 11
  • 12
16

One Line did the job for me.

df['New Column'] = 'Constant Value'
df['New Column'] = 123
Zachary
  • 179
  • 1
  • 7
11

You can Simply do the following:

df['New Col'] = pd.Series(["abc" for x in range(len(df.index))])
Imam_AI
  • 151
  • 1
  • 7
  • 2
    Thanks, that was specially good to avoid the damn chained indexing warning. – Roni Antonio Sep 16 '21 at 01:13
  • 2
    Thank you, this worked perfectly for assigning a dataframe to a column, aka `df['date'] = pd.Series([pd.date_range('2020-01-01', '2023-12-31') for x in range(len(df.index))])` – BeRT2me Apr 14 '22 at 04:24
7

This single line will work.

df['name'] = 'abc'

2

The append method has been deprecated since Pandas 1.4.0

So instead use the above method only if using actual pandas DataFrame object:

df["column"] = "value"

Or, if setting value on a view of a copy of a DataFrame, use concat() or assign():

  • This way the new Series created has the same index as original DataFrame, and so will match on exact rows
# adds a new column in view `where_there_is_one` named 
# `client` with value `display_name`
# `df` remains unchanged
df = pd.DataFrame({"number": ([1]*5 + [0]*5 )})

where_there_is_one = df[ df["number"] == 1]
where_there_is_one = pd.concat([
    where_there_is_one,
    pd.Series(["display_name"]*df.shape[0],
              index=df.index, 
              name="client")
   ], 
join="inner", axis=1)


# Or use assign
where_there_is_one = where_there_is_one.assign(client = "display_name")

Output:

where_there_is_one:                df:
| 0   | number | client       |    | 0 | number |
| --- | ---    | ---          |    |---| -------|
| 0   | 1      | display_name |    | 0 | 1      |
| 1   | 1      | display_name |    | 1 | 1      |
| 2   | 1      | display_name |    | 2 | 1      |
| 3   | 1      | display_name |    | 3 | 1      |
| 4   | 1      | display_name |    | 4 | 1      |
                                   | 5 | 0      |
                                   | 6 | 0      |
                                   | 7 | 0      |
                                   | 8 | 0      |
                                   | 9 | 0      |
Anurag Dhadse
  • 1,722
  • 1
  • 13
  • 26
0

Ok, all, I have a similar situation here but if i take this code to use: df['Name']='abc' instead 'abc' the name for the new column I want to take from somewhere else in the csv file. enter image description here

As you can see from the picture, df is not cleaned yet but I want to create 2 columns with the name "ADI dms rivoli" which will continue for every row, and the same for the "December 2019". Hope it is clear for you to understand, it was hard to explaine, sorry.

  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/32698268) – AlexK Sep 18 '22 at 22:05