简体   繁体   中英

Python Generate dummy in dataframe based on another variable

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM