[PYTHON] Convert 202003 to 2020-03 with pandas

It is a memo of the processing of the system that you do not use often but you have to google when you want to change the character string 202003 to 2020-03 of date.

From the conclusion

Suppose you have the following character string data called df, and you want to change it to the datetime module of the year and month.

Year / month
202001
202002
202003
df["Year / month"] = pd.to_datetime(df["Year / month"], format='%Y%m').dt.to_period('M')

Now you can put - and it will be a date object. format ='% Y% m' reads the string as a date module, and laterdt.to_period ('M')specifies only the year and month.

Year / month
2020-01
2020-02
2020-03

When not using dt.to_period ('M')

With the same data, try removing the last dt.to_period ('M').

Year / month
202001
202002
202003
df["Year / month"] = pd.to_datetime(df["Year / month"], format='%Y%m')

One day will be added.

Year / month
2020-01-01
2020-02-01
2020-03-01

Recommended Posts

Convert 202003 to 2020-03 with pandas
[Python] Convert list to Pandas [Pandas]
How to convert horizontally held data to vertically held data with pandas
How to convert JSON file to CSV file with Python Pandas
Convert numeric variables to categorical with thresholds in pandas
Convert .ipynb to .html (with BatchFile)
Convert list to DataFrame with python
Convert sentences to vectors with gensim
I want to do ○○ with Pandas
Convert PDF to image with ImageMagick
Convert to HSV
Convert from PDF to CSV with pdfplumber
Convert character strings to features with RoBERTa
Convert Excel data to JSON with python
Convert Hiragana to Romaji with Python (Beta)
Use pandas to convert grid data to row-holding (?) Data
Convert FX 1-minute data to 5-minute data with Python
Try converting to tidy data with pandas
Quickly try to visualize datasets with pandas
Convert PDF files to PNG files with GIMP
Convert array (struct) to json with golang
Convert HEIC files to PNG files with Python
Convert Chinese numerals to Arabic numerals with Python
Sample to convert image to Wavelet with Python
Quickly visualize with Pandas
Convert DICOM to PNG with Ascending and Descending
Try to aggregate doujin music data with pandas
Convert PDF to image (JPEG / PNG) with Python
How to use Pandas 2
Bootstrap sampling with Pandas
Processing datasets with pandas (2)
Convert PDFs to images in bulk with Python
Merge datasets with pandas
Convert mp4 to mp3 with ffmpeg (thumbnail embedded version)
Convert kanji to kana
Learn Pandas with Cheminformatics
Convert jupyter to py
[Python] How to read excel file with pandas
Convert svg file to png / ico with Python
Convert Windows epoch values to date with python
Convert keras-yolo3 to onnx
Easily convert Jupyter Notebooks to blogs with fastpages
Convert dict to array
Data visualization with pandas
Data manipulation with Pandas!
Shuffle data with pandas
Convert json to excel
How to convert (32,32,3) to 4D tensor (1,32,32,1) with ndarray type
Convert pandas dataframe elements to regular string type
Convert strings to character-by-character list format with python
Convert Mobile Suica usage history PDF to pandas Data Frame format with tabula-py
I want to convert an image to WebP with lollipop
How to convert / restore a string with [] in python
Getting Started with pandas: Basic Knowledge to Remember First
0 Convert unfilled date to datetime type with regular expression
Convert a text file with hexadecimal values to a binary file
How to convert a class object to a dictionary with SQLAlchemy
Convert comma-separated numeric strings to numbers in Pandas DataFrame
Convert the image in .zip to PDF with Python
How to access with cache when reading_json in pandas
How to extract null values and non-null values with pandas