簡體   English   中英

Pandas 將所有列轉換為 int64 類型

[英]Pandas convert ALL columns to a int64 type

我有以下代碼:

import pandas as pd

df = pd.DataFrame({'a': [2], 'b': ['1'], 'c': ['3'], 'd': [5]})
print(df.dtypes)

顯然我明白了

a     int64
b    object
c    object
d     int64
dtype: object

作為 output。我想將每一列 map 設置為 int64,但自動 - 我不想通過所有列手動設置 go 並將每一列設置為 int64。 是否有單行或狡猾的方法來做到這一點?

PS 我知道我可以使用pd.to_numeric(df['b'])將類型更改為 int64,例如。 我想對所有列執行此操作。

您可以使用pandas.DataFrame.astype

df = df.astype('int64')
print(df)

a    int64
b    int64
c    int64
d    int64
dtype: object

暫無
暫無

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

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