Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.