LoginSignup
27
16

More than 5 years have passed since last update.

read_csvでファイルを読み込む

Posted at

列のデータ型の指定(converters)

read_csvで読み込む際にconvertersを使うとデータ型を指定できる。
convertersに変換パターンを辞書型で渡す。

pd.read_csv('input_file.tsv', sep='\t', converters={'col_name_a':str, 'col_name_b':str})

通常は使うことはまず無いが、読み込みで以下のようなWarningが出た時に試すと良い。
warningの内容(パスはちょっと変更)

python3.7/site-packages/IPython/core/interactiveshell.py:2785: DtypeWarning: Columns (23,24,25,33,34,35) have mixed types. Specify dtype option on import or set low_memory=False.
  interactivity=interactivity, compiler=compiler, result=result)

ファイルは読み込めているのでWarningっぽい。

データフレームの内容を確認すると、文字列が入る列にNullの代わりに「Nan」という文字列が入っていたのでPandasがstrとfloatが混在する列だと判断したのが原因のようだった。
カラムの列数は出ているので該当箇所をすべてstrでしたらwarningは消えた。

27
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
27
16