Amy Diana
Amy Diana

Reputation: 129

Dask Add New Column Based on the Previous Column

I'm using Dask library to analyze my data.

I have this set of data and I would like to add new column which is Phone No. Flag:

ID   Phone No
001   NaN
002   O123421
003   Nan

My wish output is like this, where if the user have NaN on phone No flag as '0' else '1':

ID    Phone No   Phone No Flag
001    NaN           0
002    O123421       1
003    Nan           0

Anyone can help me on this?

Upvotes: 2

Views: 322

Answers (2)

SultanOrazbayev
SultanOrazbayev

Reputation: 16561

Dask support the regular pandas syntax, so the code below is same as one would do it in pandas:

ddf['phone_no_flag'] = ddf['phone_no'].isna() * 1

Multiplication by 1 is to get 0/1 values rather than True/False.

Upvotes: 1

RavinderSingh13
RavinderSingh13

Reputation: 133518

Could you please try following, based on your shown samples. You can make use of np.where and .isnull() functions here. Simply checking condition by np.where if column(Phone No)'s value is NaN then assign 0 to new column OR assign 1 to new column value.

import pandas as pd
import numpy as np
df1['Phone No Flag']=np.where(df1['Phone No'].isnull(),0,1)

Upvotes: 2

Related Questions