Reputation: 442
I have a large (200 columns) dataframe that has int64 and float64 columns. Without typing out all 200 column names, is it possible to convert all of the int64 to int32, and all of the float64 to float32?
Upvotes: 0
Views: 598
Reputation: 2776
Select all a particular dtype columns:
int64_cols = df.select_dtypes(['int34']).columns.tolist()
Change those columns to int32:
df[int64_cols] = df[int64_cols].astype('int32')
Upvotes: 0
Reputation: 46
You only have to create the dictionary that will be passed in the method .astype() in pandas. Let's assume that data is your pandas dataframe of 200 columns.
dtype_dict = data.dtypes.replace({'int64': 'int32', 'float64': 'float32'}).to_dict()
data = data.astype(dtype_dict)
Upvotes: 3