我有下面的数据框
customerid birthdate
8a1edbf14734127f0147356fdb1b1eb2 45
8a2ac4745091002b0150a144bcbe58b7 24
customerid是non-null object
类型的唯一标识符。但是,我希望将其转换为整数,以便能够对其进行排序和应用函数。
我使用下面的df['customerid'] = pd.to_numeric(df['customerid'], errors='coerce')
进行转换,该列现在被转换为non-null float64
。但是,ids现在是NaN
customerid birthdate
nan 45
nan 24
我基本上已经失去了客户I。如何转换为整数,并且仍然具有customerid而不是nan的值
发布于 2018-11-18 11:57:36
看起来需要将十六进制值转换为整数:
df['customerid'] = df['customerid'].apply(lambda x: int(x, 16))
print (df)
customerid birthdate
0 183593693287801188128470244383876914866 45
1 183655524454060116426046384483461912759 24
编辑:
缺少值是预期的,因为无法将非数字值(字符串)转换为数字值-参数errors='coerce'
为每个值返回NaN值:
df['customerid'] = pd.to_numeric(df['customerid'], errors='coerce')
print (df)
customerid birthdate
0 NaN 45
1 NaN 24
https://stackoverflow.com/questions/53360508
复制