Having 3 distinct values in a column and replacing them into 0/1s in pandas? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Having 3 distinct values in a column and replacing them into 0/1s in pandas? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I have the following dataframe:

d = {'col1':['a','b','c','b','a','c','c','c'],'col2':[1,2,3,4,5,6,7,8]}
df = pd.DataFrame(data=d)

I wonder how can I change 'a' to 1, 'b' to 0 and 50% of 'c' to 1 and 50% of the rest to 0 in col1 at random?

So col1 may look like this [1,0,1,0,1,0,0,1]

Answer

Compare values by c for mask by Series.eq, then use Series.map for set values by dictionary and last set 50% of values by Series.sample only filtered values:

m = df['col1'].eq('c')
df['col1'] = df['col1'].map({'a':1, 'b':0, 'c':0})

df.loc[df[m].sample(frac = 0.5).index, 'col1'] = 1

Or you can filter values and add False values by Series.reindex for mask with size like original DataFrame:

m = df['col1'].eq('c')
df['col1'] = df['col1'].map({'a':1, 'b':0, 'c':0})

mask = m[m].sample(frac = 0.5).reindex(df.index, fill_value=False)
df.loc[mask, 'col1'] = 1
print (df)
   col1  col2
0     1     1
1     0     2
2     1     3
3     0     4
4     1     5
5     1     6
6     0     7
7     0     8

Numpy solution with numpy.random.choice:

m = df['col1'].eq('c')
df['col1'] = df['col1'].map({'a':1, 'b':0, 'c':0})

df.loc[m, 'col1'] = np.random.choice([0,1], p=[0.5, 0.5], size=m.sum())
We are here to answer your question about Having 3 distinct values in a column and replacing them into 0/1s in pandas? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji