search
Search
Publish
Guest 0reps
Thanks for the thanks!
close
account_circle
Profile
exit_to_app
Sign out
search
keyboard_voice
close
Searching Tips
Search for a recipe: "Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview Doc Search Code Search Beta SORRY NOTHING FOUND!
mic
Start speaking... Voice search is only supported in Safari and Chrome.
Shrink
Navigate to
A
A
share
thumb_up_alt
bookmark
arrow_backShare Twitter Facebook

# Adding a constant number to DataFrame columns in Pandas

Programming
chevron_right
Python
chevron_right
Pandas
chevron_right
Cookbooks
chevron_right
DataFrame Cookbooks
chevron_right
Row and Column Operations Cookbook
schedule Jul 1, 2022
Last updated
local_offer PythonPandas
Tags
expand_more

We can add a constant number to DataFrame columns in Pandas using the `+` operator or the `add(~)` method.

NOTE

Unless you use the parameters `axis`, `level` and `fill_value`, `add(~)` is equivalent to performing addition using the `+` operator.

# Example

Consider the following DataFrame:

``` df = pd.DataFrame({"A":[2,3], "B":[4,5]})df A B0 2 41 3 5 ```

## All columns

To add 10 to all columns in the DataFrame:

``` df = df + 10df A B0 12 141 13 15 ```
NOTE

By default a new DataFrame is returned, so if you would like to modify the original DataFrame in-place you need to reassign the result to the original variable as in the above example.

## Single column

To add 10 to only the `"A"` column in the DataFrame:

``` df["A"] = df["A"] + 10df A B0 12 41 13 5 ```

## Missing values

Consider the following DataFrame:

``` df = pd.DataFrame({"A":[3,np.NaN], "B":[4,5]})df A B0 3.0 41 NaN 5 ```

To add 10 to the whole DataFrame:

``` df = df + 10df A B0 13.0 141 NaN 15 ```

Notice that missing values remain missing after the addition, but all other values have 10 added.

## Type error

Consider the following DataFrame:

``` df = pd.DataFrame({"A":[2,3], "B":[4,5], "C":["Sky","Towner"]})df A B C0 2 4 Sky1 3 5 Towner ```

If one of the columns you are adding a constant to is not numeric, you will get a `TypeError`:

``` df = df + 10 TypeError: can only concatenate str (not "int") to str ```