pandas:【NumPy配列から】データフレーム作成
前提
- NumPy
- np.arange
- np.reshape
サンプルコード
NumPy配列を使って作成する
import numpy as np
import pandas as pd
a = np.arange(12).reshape(3,4)
df = pd.DataFrame(a)
print(a)
print(type(a))
print(df)
print(type(df))
[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] <class 'numpy.ndarray'> 0 1 2 3 0 0 1 2 3 1 4 5 6 7 2 8 9 10 11 <class 'pandas.core.frame.DataFrame'>
- NumPy配列がそのままpandasデータフレームになった
- データタイプが「DataFrame」型になっていることを確認
列名を指定する
import numpy as np
import pandas as pd
a = np.arange(12).reshape(3,4)
df = pd.DataFrame(a, columns=['a','b','c','d'])
print(df)
a b c d 0 0 1 2 3 1 4 5 6 7 2 8 9 10 11
- columns=[xx,xx,xx]で列名を指定する
