Answers for "create pandas column with new values based on values in other columns"

3

create dataframe based on column value

df.loc[df['column_name'] == some_value]
Posted by: Guest on November-23-2020
3

pandas create new column conditional on other columns

# For creating new column with multiple conditions
conditions = [
    (df['Base Column 1'] == 'A') & (df['Base Column 2'] == 'B'),
    (df['Base Column 3'] == 'C')]
choices = ['Conditional Value 1', 'Conditional Value 2']
df['New Column'] = np.select(conditions, choices, default='Conditional Value 1')
Posted by: Guest on May-14-2020
2

pandas create a new column based on condition of two columns

conditions = [
    df['gender'].eq('male') & df['pet1'].eq(df['pet2']),
    df['gender'].eq('female') & df['pet1'].isin(['cat', 'dog'])
]

choices = [5,5]

df['points'] = np.select(conditions, choices, default=0)

print(df)
     gender      pet1      pet2  points
0      male       dog       dog       5
1      male       cat       cat       5
2      male       dog       cat       0
3    female       cat  squirrel       5
4    female       dog       dog       5
5    female  squirrel       cat       0
6  squirrel       dog       cat       0
Posted by: Guest on December-01-2020
0

sum two columns pandas

sum_column = df["col1"] + df["col2"]
Posted by: Guest on May-29-2020
0

create dataframe based on column value

df.loc[(df['column_name'] >= A) & (df['column_name'] <= B)]
Posted by: Guest on November-23-2020

Code answers related to "create pandas column with new values based on values in other columns"

Python Answers by Framework

Browse Popular Code Answers by Language