Add new column based on condition on some other column in pandas.
# np.where(condition, value if condition is true, value if condition is false)
df['hasimage'] = np.where(df['photos']!= '[]', True, False)
df.head()
Add new column based on condition on some other column in pandas.
# np.where(condition, value if condition is true, value if condition is false)
df['hasimage'] = np.where(df['photos']!= '[]', True, False)
df.head()
add multiple columns to dataframe if not exist pandas
cols = ['B','C','D']
df = df.reindex(df.columns.union(cols, sort=False), axis=1, fill_value=0)
print (df)
A B C D
0 1 2 3 0
1 4 4 5 0
2 1 2 3 0
3 4 4 6 0
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us