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

# Creating a new column based on other columns in Pandas DataFrame

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

To create a new column based on other columns, either:

# Using column arithmetics

Consider the following DataFrame:

``` df = pd.DataFrame({"A":[3,4],"B":[5,6]}, index=["a","b"])df A Ba 3 5b 4 6 ```

The fastest and simplest way of creating a new column is to use simple column-arithmetics:

``` df["C"] = df["A"] + df["B"]df A B Ca 3 5 8b 4 6 10 ```

For slightly more complicated operations, use the DataFrame's native methods:

``` df["C"] = df.max(axis=1)df A B Ca 3 5 5b 4 6 6 ```

Note the following:

• we are populating the new column `C` with the maximum of each row (`axis=1`).

• the return type of `df.max(axis=1)` is `Series`.

# Using assign method

Consider the same DataFrame as before:

``` df = pd.DataFrame({"A":[3,4],"B":[5,6]}, index=["a","b"])df A Ba 3 5b 4 6 ```

For more flexibility, use DataFrame's `assign(~)` method:

``` def foo(df): if df["A"].sum() > df["B"].sum(): return [-1,-1] else: return [0,0]df.assign(C=foo) A B C0 3 5 01 4 6 0 ```

Note the following:

• if the sum of column `A` is larger than that of column `B`, then `[-1,-1]` will be used as the new column, otherwise `[0,0]` will be used.

• the keyword argument (`C`) became the new column label.