Answers for "create a dataframe with one column from another dataframe pandas"

2

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

create new dataframe with columns from another dataframe pandas

new = old[['A', 'C', 'D']].copy()
Posted by: Guest on March-24-2021

Code answers related to "create a dataframe with one column from another dataframe pandas"

Python Answers by Framework

Browse Popular Code Answers by Language