pandas の DataFrame に変換する
Python
Published: 2019-06-30

やったこと

pandas で 配列データ、辞書データを DataFrame に変換してみます。

確認環境

$ ipython --version
6.1.0
$ jupyter --version
4.3.0
$ python --version
Python 3.6.2 :: Anaconda custom (64-bit)
import pandas as pd
print(pd.__version__)
0.20.3

調査

from sklearn import datasets
iris = datasets.load_iris()
train = iris.data

print(type(train))

df = pd.DataFrame(train)
print(type(df))

data : ndarray (structured or homogeneous), Iterable, dict, or DataFrame Dict can contain Series, arrays, constants, or list-like objects

引数に渡すデータは ndarray だけでなくても良いようです。

リストを渡す

import pandas as pd
train = [2, 3]
print(type(train))

df = pd.DataFrame(train)
print(type(df))
<class 'list'>
<class 'pandas.core.frame.DataFrame'>

辞書を渡す

import pandas as pd
train = {'col1': [1, 2], 'col2': [3, 4]}
print(type(train))

df = pd.DataFrame(train)
print(type(df))
<class 'dict'>
<class 'pandas.core.frame.DataFrame'>

参考