我有以下代码:
import pandas as pd
df = pd.DataFrame({'a': [2], 'b': ['1'], 'c': ['3'], 'd': [5]})
print(df.dtypes)
Run Code Online (Sandbox Code Playgroud)
显然我明白了
a int64
b object
c object
d int64
dtype: object
Run Code Online (Sandbox Code Playgroud)
作为输出。我想将每一列映射到 int64,但是自动映射 - 我不想手动遍历所有列并将它们每一列设置为 int64。有没有一种简单或巧妙的方法可以做到这一点?
PS 我知道我可以通过使用将类型更改为 int64 pd.to_numeric(df['b']),例如。我想对所有列执行此操作。
df = df.astype('int64')
Run Code Online (Sandbox Code Playgroud)
print(df)
a int64
b int64
c int64
d int64
dtype: object
Run Code Online (Sandbox Code Playgroud)