Answers for "create a new column in pandas based on multiple condition of another column"

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
0

df multiple columns into one column

df.stack().reset_index()

   level_0   level_1  0
0        0  Column 1  A
1        0  Column 2  E
2        1  Column 1  B
3        1  Column 2  F
4        2  Column 1  C
5        2  Column 2  G
6        3  Column 1  D
7        3  Column 2  H
Posted by: Guest on July-27-2021

Code answers related to "create a new column in pandas based on multiple condition of another column"

Python Answers by Framework

Browse Popular Code Answers by Language