I'd like to add a column with binary value that mirrors the - from multiple columns. I've found several posts that convert integers/strings to binary value, however, they are separated into multiple columns instead of a single column, eg. 01100 in a column. Below is a sample df.:
original df:
-------- -------- -------- -------- -------- | te1_pl | te2_pl | te3_pl | te4_pl | te5_pl | -------- -------- -------- -------- -------- | -10 | -32 | 305.3 | -69 | -44 | | -7 | 69 | -14 | 41 | 41 | | 47 | 16 | -38 | -9 | 82 | | 1 | 18 | 37 | 0 | 42 | | 9 | 36 | 47 | -33 | 6 | | -18 | 4.3 | 16.5 | 28.1 | -9.9 | -------- -------- -------- -------- --------
output hope to get:
-------- -------- -------- -------- -------- ------- | te1_pl | te2_pl | te3_pl | te4_pl | te5_pl | bin | -------- -------- -------- -------- -------- ------- | -10 | -32 | 305.3 | -69 | -44 | 00100 | | -7 | 69 | -14 | 41 | 41 | 01011 | | 47 | 16 | -38 | -9 | 82 | 11001 | | 1 | 18 | 37 | 0 | 42 | 11101 | | 9 | 36 | 47 | -33 | 6 | 11101 | | -18 | 4.3 | 16.5 | 28.1 | -9.9 | 01110 | -------- -------- -------- -------- -------- -------
data = {
"te1_pl": [-10,-7,47,1,9,-18,],
"te2_pl": [-32,69,16,18,36,4.3,],
"te3_pl": [305.3,-14,-38,37,47,16.5,],
"te4_pl": [-69,41,-9,0,-33,28.1,],
"te5_pl": [-44,41,82,42,6,-9.9,],}
df = pd.DataFrame(data)
kindly advise
CodePudding user response:
bin_all = df.apply(lambda x: x.apply(lambda y: "1" if y > 0 else "0"))
df["bin"] = bin_all.T.apply(lambda x: "".join(x))
df
# te1_pl te2_pl te3_pl te4_pl te5_pl bin
#0 -10 -32.0 305.3 -69.0 -44.0 00100
#1 -7 69.0 -14.0 41.0 41.0 01011
#2 47 16.0 -38.0 -9.0 82.0 11001
#3 1 18.0 37.0 0.0 42.0 11101
#4 9 36.0 47.0 -33.0 6.0 11101
#5 -18 4.3 16.5 28.1 -9.9 01110