410

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

10 Answers 10

569

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
Sign up to request clarification or add additional context in comments.

1 Comment

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

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

1 Comment

This gives more control on the position of the new column.
100

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

1 Comment

Plus one for assign because it avoids mutating the underlying data frame.
67

Single liner works

df['Name'] = 'abc'

Creates a Name column and sets all rows to abc value

2 Comments

That gives me a warning A value is trying to be set on a copy of a slice from a DataFrame.
df['New Col'] = pd.Series(["abc" for x in range(len(df.index))]) example below show
38

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.

Comments

17

One Line did the job for me.

df['New Column'] = 'Constant Value'
df['New Column'] = 123

1 Comment

For me this adds a new row, not a column.
11

You can Simply do the following:

df['New Col'] = pd.Series(["abc" for x in range(len(df.index))])

2 Comments

Thanks, that was specially good to avoid the damn chained indexing warning.
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))])
7

This single line will work.

df['name'] = 'abc'

1 Comment

gives a warning
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      |

Comments

0

While df[name] = value works, it might not create a Series of the data type you desire. When dealing with large numerical arrays, you want to avoid wasting 64 bits on what could fit in just 8 and also bypass unnecessary type conversions .astype(), which can fragment and waste your memory too.

To assign a constant column of a specific data type, you can write something like:

df[name] = pd.Series(0, index=df.index, dtype='Int8')

In this example, we create a pandas Series around the existing DataFrame's index and assign it to a column name. The column is of a nullable integer type (note the capital "I" in Int8) and is filled with all zeros.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.