feinmann feinmann - 3 months ago 13
Python Question

Pandas: Fill new column by condition row-wise

import pandas as pd
import numpy as np

df = pd.DataFrame([np.random.rand(100),100*[0.1],100*[0.3]]).T
df.columns = ["value","lower","upper"]

df.head()


enter image description here

How can I create a new column which indicates that
value
is between
lower
and
upper
?

Answer

You can use between for this purpose.

df['new_col'] = df['value'].between(df['lower'], df['upper'])