我在数据框中有以下一列:
COLUMN_NAME
1
0
1
1
65280
65376
65280
我想将一列中的5位数字值转换为其相应的二进制值。我知道如何使用bin()
函数进行转换,但我不知道如何仅将其应用于具有5位数字的行。
请注意,该列仅包含1或5位数字的值。具有1位数字的值只能是1或0。
答案 0 :(得分:0)
import pandas as pd
import numpy as np
data = {'c': [1,0,1,1,65280,65376,65280] }
df = pd.DataFrame (data, columns = ['c'])
// create another column 'clen' which has length of 'c'
df['clen'] = df['c'].astype(str).map(len)
//check condition and apply bin function to entire column
df.loc[df['clen']==5,'c'] = df['c'].apply(bin)