Whole type conversion for each dataframe column in python

Consider type conversion of dataframe

I don't think there is much demand, but ... The environment is supposed to run on anaconda. You may want to change the type when you import csv etc. into dataframe with python. There is a way to take out the values one by one, convert the type and put it back in, but that is difficult. If possible, I would like to convert each column at once. For example, suppose you have the following table and you have imported this csv data.

name age birthday
johan 21 1999-01-01
maria 22 1999-01-02
johan 23 1999-01-03

Convert from number to string

I don't think there is much demand, but I would like to convert numbers to strings.

import pandas as pd
import datetime

test_data = pd.read_csv("test.csv")
test_data['age'] = test_data['age'].astype('str')
age = test_data['age'][0]

I think that the age column is int type because it is cast in the data frame. After that, astype is used to convert the cast type for each column. To be honest, I don't think there is much use for this.

Make date data a time stamp

Let's take a look at the date data of the previous data. I think it has become a character string type. I think this is difficult to handle as data, so I will convert it to a time stamp.

test_data['birthday'] = pd.to_datetime(test_data['birthday'])


It was a summary that I wanted to see by converting the type for each column of the data frame.

Recommended Posts

Whole type conversion for each dataframe column in python
Type annotations for Python2 in stub files!
Search for strings in Python
Techniques for sorting in Python
Image data type conversion [Python]
About "for _ in range ():" in python
Check the operation of Python for .NET in each environment
How to find the cumulative sum / sum for each group using DataFrame in Spark [Python version]
Check for memory leaks in Python
Check for external commands in python
Function argument type definition in python
Python for super beginners Python # dictionary type 1 for super beginners
Automatically resize screenshots for the App Store for each screen in Python
Dynamically load json type in python
Type specified in python. Throw exceptions
Run unittests in Python (for beginners)
Python for super beginners Python # dictionary type 2 for super beginners
Prepare Python development environment for each project in Windows environment (VSCode + virtualEnvWrapper + Pylint)
How to find a specific type (str, float etc) column in a DataFrame column
Switch the module to be loaded for each execution environment in Python
Conversion of string <-> date (date, datetime) in Python
Notes on nfc.ContactlessFrontend () for nfcpy in python
Comparison of Japanese conversion module in Python3
Python learning basics ~ What is type conversion? ~
Tips for dealing with binaries in Python
Summary of various for statements in Python
In python + sqlite3 "OperationalError: no such column:"
Template for writing batch scripts in python
Process multiple lists with for in Python
MongoDB for the first time in Python
Get a token for conoha in python
Sample for handling eml files in Python
AtCoder cheat sheet in python (for myself)
Code required for API conversion in sqlalchemy
I searched for prime numbers in python
Notes for using python (pydev) in eclipse
Python: Preprocessing in machine learning: Data conversion
Tips for making small tools in python
Use pathlib in Maya (Python 2.7) for upcoming Python 3.7
Status of each Python processing system in 2020
String → Bool value conversion in Python Consideration
Type notes to Python scripts for running PyTorch model in C ++ with libtorch
Template for creating command line applications in Python
CERTIFICATE_VERIFY_FAILED in Python 3.6, the official installer for macOS
++ and-cannot be used for increment / decrement in python
Import-linter was useful for layered architecture in Python
Type Python scripts to run in QGIS Processing
Match the distribution of each group in Python
Python pandas: Search for DataFrame using regular expressions
Get multiple maximum keys in Python dictionary type
Add quotation marks ">" for replying emails in Python3
Static type checking that starts loosely in Python
Logging settings for daily log rotation in python
Tips for hitting the ATND API in Python
Try to calculate RPN in Python (for beginners)
Notes for implementing simple collaborative filtering in Python
Don't use readlines () in your Python for statement!
[Implementation for learning] Implement Stratified Sampling in Python (1)
Settings for Python coding in Visual Studio Code
Kernel / Python version summary for each Debian release
How to handle datetime type in python sqlite3