Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2021 12:59 PM
You an do something like this in pandas. Note there could be a more performant way to do this too.
import pandas as pd
import numpy as np
df = pd.DataFrame({'a':[1,2,3,4]})
df.head()
> a
> 0 1
> 1 2
> 2 3
> 3 4
conditions = [(df['a'] <=2), (df['a']>2)]
values = ['value1', 'value2']
df['b'] = np.select(conditions, values)
df.head()
a b
> 0 1 value1
> 1 2 value1
> 2 3 value2
> 3 4 value2