pandas conditionally fill values with 0 and 1

Question:

Doing the following conditional fill in pyspark how would I do this in pandas

colIsAcceptable = when(col("var") < 0.9, 1).otherwise(0)
Asked By: muhammad

||

Answers:

colIsAcceptable = df['var'].apply(lambda x: 1 if x < 0.9 else 0)

apply can be slow on very large datasets, and there are more efficient ways that I don’t know of, but is good for general purposes

You can use:

df['new_col'] = df['col'].lt(0.9).astype(int)

or with numpy.where:

import numpy as np
df['new_col'] = np.where(df['col'].lt(0.9), 1, 0)
Answered By: mozway

You can use numpy.where.

import numpy as np
df['colIsAcceptable'] = np.where(df['col'] < 0.9, 1, 0)
Answered By: I'mahdi
df['col2'] = 0
df.loc[df['col1'] < 0.9, 'col2'] = 1

This is a simple example to do something like what you are asking.

Answered By: Killian Fortman

I assume the first column on your dataframe is named ‘var’. and then the second column name is ‘colIsAcceptable’, then you can use .map() function


df['colIsAcceptable']= df['var'].map(lambda x: 1 if x<0.9 else 0)

Answered By: Yusuf Syam
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.