Currently trying to change some market values in a dataframe if some value is present in another column. I'm only familiar with list comprehension for creating new columns, but im not sure its possible to do it this way.
I tried the following function, though it doesn't work:
def multiply_with(x):
if df['a'] in list_multiple_double:
x = x * 0.5
elif df['a'] in list_multiple_triple:
x = x * 0.33
else:
x = x
return x
I tried applying this on the market value column, but no luck.
Assume there are two columns a and b in a pd.dataframe, and i would like to create c. Also assume 2 lists, list_a
and list_b
. For every observation, if a
is present in list_a
then multiply by 0.5 and return this in column c
. If a
is present in list_
b then multiply by 0.333 and return in c
. If not present either return a
in c
. I need the following:
list_a = ['Denmark Sweden', 'Norway Sweden']
list_b = ['Denmark Sweden Norway']
values = [200,300,500,1000,200]
country = ['Denmark', 'Denmark Sweden', 'Denmark Sweden Norway', 'Sweden', 'Norway']
values_adj = [200, 150, 166.67, 1000, 200]
df = pd.DataFrame()
df['values'] = pd.Series(values)
df['country'] = pd.Series(country)
df['values_adj'] = pd.Series(values_adj)