[PYTHON] Type conversion of multiple columns of pandas DataFrame with astype at the same time

As it may have been common sense for those who know it, the argument of .astype () can be specified in a dictionary.

from numpy.random import rand
from pandas import DataFrame

df = DataFrame(rand(3, 3), columns=[list('abc')])
df.dtypes
a    float64
b    float64
c    float64
dtype: object
print(df.astype({'a': int, 'c': str}))
   a         b                 c
0  0  0.692601   0.0963216888725
1  0  0.873724  0.00960078591002
2  0  0.536855    0.639548536613
print(df.astype({'a': int, 'c': str}).dtypes)
a      int64
b    float64
c     object
dtype: object

Recommended Posts

Type conversion of multiple columns of pandas DataFrame with astype at the same time
Calculate the time difference between two columns with Pandas DataFrame
Browse .loc and .iloc at the same time in pandas DataFrame
Plot multiple maps and data at the same time with Python's matplotlib
Turn multiple lists with a for statement at the same time in Python
Get the sum of each of multiple columns with awk
Reformat the timeline of the pandas time series plot with matplotlib
Extract specific multiple columns with pandas
How to change multiple columns of csv in Pandas (Unixtime-> Japan Time)
Count the number of times two values appear in a Python 3 iterator type element at the same time
Set up a server that processes multiple connections at the same time
I compared the moving average of IIR filter type with pandas and scipy
Loop variables at the same time in the template
Update multiple tables at once with pandas to_sql
At the time of python update on ubuntu
[Python] Determine the type of iris with SVM
Find the sum of unique values with pandas crosstab
The story of sharing the pyenv environment with multiple users
Visualize data and understand correlation at the same time
Adjust the ratio of multiple figures with the matplotlib gridspec
I tried to automatically post to ChatWork at the time of deployment with fabric and ChatWork Api
I tried the same data analysis with kaggle notebook (python) and Power BI at the same time ②
I tried the same data analysis with kaggle notebook (python) and Power BI at the same time ①
Learning record (6th day) #Set type #Dictionary type #Mutual conversion of list tuple set #ndarray type #Pandas (DataFrame type)
Python built-in function ~ divmod ~ Let's get the quotient and remainder of division at the same time
Check what line caused the error with apply () (dataframe, Pandas)
wxPython: Draw animation and graph drawing at the same time
Get UNIXTIME at the beginning of today with a command
Let's execute the command on time with the bot of discord
Grep so that grep does not appear at the time of grep
[Data science memorandum] Confirmation of the contents of DataFrame type [python]
Basic calculation of pandas to enjoy Hakone Ekiden while competing with the best members of all time
python memo: enumerate () -get index and element of list at the same time and turn for statement