[英]Convert all columns from int64 to int32
We all now the question: Change data type of columns in Pandas where it is really nice explained how to change the data type of a column, but what if I have a dataframe df
with the following df.dtypes
:我们现在都有一个问题: 在 Pandas中更改列的数据类型真的很好解释了如何更改列的数据类型,但是如果我有一个具有以下df.dtypes
的数据df.dtypes
df
df.dtypes
:
A object
B int64
C int32
D object
E int64
F float32
How could I change this without explicity mention the column names that all int64
types are converted to int32
types?如果不明确提及所有int64
类型都转换为int32
类型的列名,我该如何更改? So the desired outcome is:所以想要的结果是:
A object
B int32
C int32
D object
E int32
F float32
You can create dictionary by all columns with int64
dtype by DataFrame.select_dtypes
and convert it to int32
by DataFrame.astype
, but not sure if not fail if big integers numbers:您可以创建字典与所有列int64
通过D型DataFrame.select_dtypes
并将其转换为int32
由DataFrame.astype
,但不知道如果没有大的整数数字失败:
df = pd.DataFrame({
'A':list('abcdef'),
'B':[4,5,4,5,5,4],
'C':[7,8,9,4,2,3],
'D':[1,3,5,7,1,0],
'E':[5,3,6,9,2,4],
'F':list('aaabbb')
})
d = dict.fromkeys(df.select_dtypes(np.int64).columns, np.int32)
df = df.astype(d)
print (df.dtypes)
A object
B int32
C int32
D int32
E int32
F object
dtype: object
Use DataFrame.select_dtypes
and DataFrame.astype
:使用DataFrame.select_dtypes
和DataFrame.astype
:
# example dataframe
df = pd.DataFrame({'A':list('abc'),
'B':[1,2,3],
'C':[4,5,6]})
A B C
0 a 1 4
1 b 2 5
2 c 3 6
# as we can see, the integer columns are int64
print(df.dtypes)
A object
B int64
C int64
dtype: object
df = df.astype({col: 'int32' for col in df.select_dtypes('int64').columns})
# int64 columns have been converted to int32
print(df.dtypes)
A object
B int32
C int32
dtype: object
You can also use a for loop to iterate through the df's columns and check their datatype.您还可以使用 for 循环遍历 df 的列并检查它们的数据类型。
df = pd.DataFrame({'A': ['a','b','c'],
'B': [1,2,3],
'C': [4.0,5.0,6.0]})
print(df.dtypes)
A object
B int64
C float64
dtype: object
#if it's int64 set it as int32:
for column in df.columns:
if df[column].dtype == 'int64':
df[column] = df[column].astype('int32')
print(df.dtypes)
A object
B int32
C float64
dtype: object
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.