亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在熊貓中將對象轉換為 int 或 float

如何在熊貓中將對象轉換為 int 或 float

慕的地8271018 2023-05-16 14:35:07
在加載的 excel 文件上使用 df.info 方法后,我得到以下數據<class 'pandas.core.frame.DataFrame'>RangeIndex: 30000 entries, 1 to 30000Data columns (total 25 columns): #   Column      Non-Null Count  Dtype ---  ------      --------------  -----  0   Unnamed: 0  30000 non-null  object 1   X1          30000 non-null  object 2   X2          30000 non-null  object 3   X3          29669 non-null  object 4   X4          29677 non-null  object 5   X5          30000 non-null  object 6   X6          30000 non-null  object 7   X7          30000 non-null  object 8   X8          30000 non-null  object 9   X9          30000 non-null  object 10  X10         30000 non-null  object 11  X11         30000 non-null  object 12  X12         30000 non-null  object 13  X13         30000 non-null  object 14  X14         30000 non-null  object 15  X15         30000 non-null  object 16  X16         30000 non-null  object 17  X17         30000 non-null  object 18  X18         30000 non-null  object 19  X19         30000 non-null  object 20  X20         30000 non-null  object 21  X21         30000 non-null  object 22  X22         30000 non-null  object 23  X23         30000 non-null  object 24  Y           30000 non-null  objectdtypes: object(25)memory usage: 2.9+ MB我不知道為什么所有數據類型都是對象,盡管它們中的大多數都有數值 如何修復我的數據集的數據類型
查看完整描述

2 回答

?
慕姐8265434

TA貢獻1813條經驗 獲得超2個贊

讓我們試試to_numeric


df = pd.DataFrame({'1':['1','2'],'2':['a','b']})

df = df.apply(pd.to_numeric,errors='ignore')

查看


df.info()

<class 'pandas.core.frame.DataFrame'>

RangeIndex: 2 entries, 0 to 1

Data columns (total 2 columns):

 #   Column  Non-Null Count  Dtype 

---  ------  --------------  ----- 

 0   1       2 non-null      int64 

 1   2       2 non-null      object

dtypes: int64(1), object(1)

memory usage: 88.0+ bytes


查看完整回答
反對 回復 2023-05-16
?
神不在的星期二

TA貢獻1963條經驗 獲得超6個贊

嘗試例如:

df['X1'] = df['X1'].astype(str).astype(int)

如果要格式化所有列,請嘗試:

df = df.astype(int)

這是因為,當您導入.csv文件時,大部分列都被轉換為對象。


查看完整回答
反對 回復 2023-05-16
  • 2 回答
  • 0 關注
  • 162 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號