Ask Your Question
2

How to add a new column in Pandas by selectively combining the values of two existing columns depending on certain values in another column?

asked 2022-06-30 11:00:00 +0000

plato gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-12-25 17:00:00 +0000

david gravatar image

To add a new column in Pandas by selectively combining the values of two existing columns depending on certain values in another column, you can use the apply function in combination with a lambda function.

Here's an example code snippet:

import pandas as pd

# create a sample dataset
data = {
    'Name': ['John', 'Alice', 'Bob', 'Charlie'],
    'Age': [26, 29, 32, 27],
    'Gender': ['Male', 'Female', 'Male', 'Male'],
    'Nationality': ['USA', 'France', 'UK', 'Canada']
}

df = pd.DataFrame(data)

# define a lambda function that selectively combines the values of two columns
new_column = df.apply(lambda row: row['Name'].upper() if row['Gender'] == 'Male' else str(row['Age']) + '_' + row['Nationality'], axis=1)

# add the new column to the dataframe
df['New Column'] = new_column

# print the updated dataframe
print(df)

In this example, we're creating a new column called "New Column" by selectively combining the values of the "Name" and "Age" columns depending on the value of the "Gender" column. If the gender is "Male", we're taking the uppercase of the name. Otherwise, we're combining the age and nationality columns with an underscore. The apply function is used to apply this lambda function to each row of the dataframe. Finally, we're adding the new column to the original dataframe using the bracket notation.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-06-30 11:00:00 +0000

Seen: 17 times

Last updated: Dec 25 '22