python pandasでDataFrameを作成する

pythonで、ライブラリpandasのDataFrameを使用して、DataFrameを作成するサンプルコードを記述してます。pythonのバージョンは3.8.5を使用してます。
環境
- OS windows10 pro 64bit
- python 3.8.5
pandasインストール
pandasをインストールされていない方は、pipでインストールしておきます。
pip install pandas
# Successfully installed pandas-1.1.4
DataFrame使い方
DataFrameを使用すると、DataFrameを作成するが可能です。
import pandas as pd
d = pd.DataFrame(data)
以下は、DataFrameを作成するサンプルコードとなります。
import pandas as pd
# データフレーム作成
d = pd.DataFrame({
'a': [11, 12, 13],
'b': [21, 22, 23],
'c': [31, 32, 33]
})
print(d)
# a b c
# 0 11 21 31
# 1 12 22 32
# 2 13 23 33
列の値は、以下で取り出すことが可能です。
print(d['a'])
# 0 11
# 1 12
# 2 13
# Name: a, dtype: int64
indexを指定することも可能です。
import pandas as pd
# データフレームの初期化
d = pd.DataFrame({
'a': [11, 12, 13],
'b': [21, 22, 23],
'c': [31, 32, 33]
},
index = ['1', '2', '3']
)
print(d)
# a b c
# 1 11 21 31
# 2 12 22 32
# 3 13 23 33
-
前の記事
C# treeViewにノード(階層)を追加する 2021.07.17
-
次の記事
javascript lodashを使って指定した文字列を置換する 2021.07.17
コメントを書く