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()
how to assign a new value in a column in pandas dataframe
df = df.assign(industry='yyy')
add a value to an existing field in pandas dataframe after checking conditions
gapminder['gdpPercap_ind'] = gapminder.gdpPercap.apply(lambda x: 1 if x >= 1000 else 0)
gapminder.head()
new column in pandas with where logic
virtsizes = {
"type1": { "gb": 1.2, "xxx": 0, "yyy": 30 },
"type2": { "gb": 1.5, "xxx": 2, "yyy": 20 },
"type3": { "gb": 2.3, "xxx": 0.1, "yyy": 10 },
}
d = {k:v['gb'] for k,v in virtsizes.items()}
print (d)
{'type2': 1.5, 'type1': 1.2, 'type3': 2.3}
df = pd.DataFrame({'vol-type':['type1','type2']})
df["real_size"] = df["vol-type"].map(d)
print (df)
vol-type real_size
0 type1 1.2
1 type2 1.5
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