Pass dataframe containing True / False from Python to R in csv format (pd.DataFrame-> tbl_df)

It's not a big deal, but I was addicted to saving pandas.DataFrame as csv and reading it with R reader :: tbl_df, so make a note of the workaround.

When dealing with small and medium-sized data frames, I think it is common to use pandas for Python and data.frame for R.

Also, passing dataframes between Python <=> R may mediate SQL, but I think csv is better if you do it easily.

Problems of delivery by csv

However, when pandas.DataFrame including bool is spit out to csv as it is, it seems that it cannot be read as logical with read \ _csv. Like this ↓

from datetime import datetime
import pandas as pd

df = pd.DataFrame({
    'A': ('a1', 'a2', 'a3'),
    'B': (True, False, True),
    'C': (0, 1, 2),
    'D': [datetime.now()] * 3
})

df.to_csv('sample.csv', index=False, encoding='utf-8')
library(readr)

read_csv('sample.csv', col_types = 'cliT', locale = locale(encoding = 'UTF-8'))

スクリーンショット 2017-04-23 11.39.05.png

Looking at the error, it seems that only T / F, TRUE / FALSE, and 0/1 are accepted as logical.

Workaround

# df.to_csv('sample.csv', index=False, encoding='utf-8')
(df * 1).to_csv('sample.csv', index=False, encoding='utf-8')

You can do it. It sets True / False to 1/0. \ * For a character string is a process that changes "" hoge "\ * 2" to "" hogehoge "", so even if you "\ * 1" like this time, nothing will change.

スクリーンショット 2017-04-23 11.44.25.png

If it is 01, it can be read with read \ _csv.

スクリーンショット 2017-04-23 11.53.09.png

Failure example

By the way, the following method fails.

df.astype(int)  #Fail if there is str etc.
df.replace({True: 1, False: 0})  #Nothing happens
df.replace({True: "TRUE", False: "FALSE"})  # 1/0s are all strings(Figure below)

スクリーンショット 2017-04-23 11.50.58.png

(Please tell me if there is another good way)

Recommended Posts

Pass dataframe containing True / False from Python to R in csv format (pd.DataFrame-> tbl_df)
How to read csv containing only integers in Python
[R] [Python] Memo to read multiple csv files in multiple zip files
Convert XML document stored in XML database (BaseX) to CSV format (using Python)
How to do R chartr () in Python
Python> Output numbers from 1 to 100, 501 to 600> For csv
Convert from Markdown to HTML in Python
[Python] Convert from DICOM to PNG or CSV
From file to graph drawing in Python. Elementary elementary
[python] Create table from pandas DataFrame to postgres
Csv in python
format in python
I want to write in Python! (1) Code format check
App development to tweet in Python from Visual Studio 2017
Store csv on GCS from AI platform in DataFrame
How to download files from Selenium in Python in Chrome
Convert / return class object to JSON format in Python
Execute Python function from Powershell (how to pass arguments)
Python script to create a JSON file from a CSV file
python> datetime> From date string (ISO format: 2015-12-09 12:40:08) to datetime type
I want to use the R dataset in python
Remove headings from multiple format CSV files with python
[python] Use DataFrame to label arbitrary variables and arrays together and save them in csv [pandas]
2015-12-26 python2> datetime> Implementation to take the difference in seconds from two ISO format datetime strings> Use .seconds ()