簡體   English   中英

如何在python中將數據框列轉換為int

[英]how do you convert data frame column to int in python

我正在嘗試將數據類型轉換為python中的inter

當我做

df1.dtypes

我得到這個:

max          object

我需要將df1 ['max']轉換為int

當我這樣做時:

df1["max"]=df1['max'].astype(int)

我收到此錯誤:

ValueError: invalid literal for long() with base 10: '312.72857666015625'

我試圖在轉換前將最大值取整:

df1[['max']] = df1[['max']].apply(lambda x: pd.Series.round(x, 2))

我收到此錯誤:

TypeError: ("can't multiply sequence by non-int of type 'float'", u'occurred at index max')

在Pyton Pandas中有一種簡單的方法嗎?

如果您只想將str轉換為int

s.astype(float).astype(int)
Out[213]: 
0    312
dtype: int32

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM