Skip to content Skip to sidebar Skip to footer

How To Encode A Pandas Row With Custom Values?

I would like to encode the row values of the selected column in pandas. For example, let's assume I have the following dataframe Col_A Col_B 0 SV NT 1 NT P 2

Solution 1:

You can apply and map using column names:

print (df.apply(lambda d: d.map(encode_values[d.name])))

   Col_A  Col_B
001011102003104010

Solution 2:

Try replace with dictionary as parameter, apply separately to each series in the df:

df.Col_A = df.Col_A.replace (encode_values['Col_A'])
df.Col_B = df.Col_B.replace (encode_values['Col_B'])

Post a Comment for "How To Encode A Pandas Row With Custom Values?"