Answers for "pandas make new column from other columns"

1

pandas create column from another column

# Creates a new column 'blue_yn' based on the existing 'color' column
# If the 'color' column value is 'blue' then the new column value is 'YES'
df['blue_yn'] = np.where(df['color'] == 'blue', 'YES', 'NO')
# Can also do this using .apply and a lambda function
df['blue_yn']= df['color'].apply(lambda x: 'YES' if (x == 'blue') else 'NO')
Posted by: Guest on August-15-2021
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

select columns to include in new dataframe in python

new = old.filter(['A','B','D'], axis=1)
Posted by: Guest on March-02-2020
0

How to Create new dataframe columns from existing column

new_df = df.filter(like='n_') 
           .replace(0., np.inf) 
           .apply(lambda x: sorted(x), axis=1, result_type='expand') 
           .replace(np.inf, 0.0)

new_df.columns = ['new_1', 'new_2', 'new_3']

out = pd.concat([df, new_df], axis=1)
Posted by: Guest on September-22-2021

Code answers related to "pandas make new column from other columns"

Python Answers by Framework

Browse Popular Code Answers by Language