简体   繁体   中英

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 :

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? 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:

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 :

# 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM