我试图用数字除后截断pandas中的列值。这是我尝试过的两种方法:
df['new'] = int('%.0f'%(df['old']/12))
df['new'] = int(df['old']/12)
每次出现输入错误:
cannot convert the series to <class 'float'>
cannot convert the series to <class 'int'>
如何在避免这些错误的同时解决此问题?
答案 0 :(得分:1)
代码需要将除法生成的Series
转换为int
。
df['new'] = (df['old'] / 12).astype(int)