在pandas数据框中将对象转换为int类型

时间:2020-05-03 14:39:30

标签: python pandas

我有一个数据框

remote: Temporary clone tokens are read-only.
fatal: unable to access 'https://github.com/myusername/repositoryname.git/': The requested URL returned error: 403
print(df.dtypes)

我正在尝试创建一个值为'Total Bytes'的数据透视表,并将'Total Bytes'列转换为int但出现错误

Index                                                      object
Month Id                                                   int64
Customer - ID                                              object
Billed Customer - Name                                     object
Status - -Customer                                         object
Original Contract Id  -                                    object
NIcode - NI Code                                           int64
Total Bytes                                                object
dtype: object
df['Total Bytes']=pd.to_numeric(df['Total Bytes'])

“总字节数”中的一些值是:

ValueError: invalid literal for int() with base 10: '3,429,155,997.00'

请帮忙。

1 个答案:

答案 0 :(得分:-1)

df['Total Bytes'] = df['Total Bytes'].apply(lambda x: x.replace(',',''))
df['Total Bytes'] = pd.to_numeric(df['Total Bytes'])