LoginSignup
3
1

More than 3 years have passed since last update.

Pandas個人的注意点まとめ

Posted at

概要

自分用に使っていて注意したほうがいい点をまとめる。
順次追加予定。

indexの型に関して

CSVの読み込み時index_colを利用するとindexの型はdtypeの指定とは別に
自動で決定されてしまうようだ。

import pandas as pd

# 例) input.csv
#ID,param1,param2
#0001,01,AAA
#0002,02,BBB
#0003,10,CCC

df = pd.read_csv("input.csv", dtype=object, index_col="ID")
#型をobjectに指定しているがindexだけはint型となってしまい
#先頭の0が抜け落ちてしまう

型を保持するためにはindexの指定を後から行う必要があるようだ。

df = pd.read_csv("input.csv", dtype=object)
df.set_index("ID", inplace=True)
#これで元のデータのまま保持できる
3
1
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
3
1