0
1

More than 3 years have passed since last update.

DataFrameでインデックスとカラムを設定する

Posted at
1 / 2

概要

DataFrameでは、行の名前をインデックス、列の名前をカラムと呼ぶ。

indexを指定なしで作成した際、インデックスは0から割り当てられる。
import pandas as pd

fruits = ["apple", "orange", "banana", "strawberry"]
data1 = [10, 5, 8, 12]
data2 = [30, 25, 12, 10]
series1 = pd.Series(data1, index = fruits)
series2 = pd.Series(data2, index = fruits)

df = pd.DataFrame([series1, series2])

display(df)

スクリーンショット 2021-07-14 20.34.38.png

indexの指定あり
df.index = ["name1", "name2"]

スクリーンショット 2021-07-14 20.33.25.png

補足

・カラムは元データのSeriesのindexや辞書型のキーになる。
・インデックスはdf.indexに行数と同じ長さのリストを、カラムはdf.columnsに列数と同じ長さのリストを、それぞれ代入する事が可能。

0
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
0
1