0

I have dataframe with many variables. I would like to generate a dummy variable based on column 1, for example. If column 1's observation is NaN, then the dummy variable is filled with 0. If column 1' observation is not missing, then the dummy variable is filled with 1. Any ideas? Thanks a lot.

1
  • Use isna method and convert the result to int Commented Mar 15, 2021 at 16:17

1 Answer 1

2

This is the easiest way:

# sample data
import pandas as pd 
import numpy as np
df = pd.DataFrame()
df['sample'] = [1,2,np.nan,4,5,np.nan]

# create dummy column
df['dummy'] = np.where(df['sample'].isna(),0,1)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.