0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Python Series入門:インストールからデータフレーム作成までの基礎

Posted at

PythonのPandasライブラリはデータの操作や解析に非常に便利なツールで、特にデータフレームを使ったデータ管理は非常に直感的です。

Pandasのインストール

PandasはPythonのパッケージ管理ツールであるpipを使ってインストールできます。

pip install pandas

Pandasのインポート

まず、Pandasをインポートします。Pandasはデータ操作に必要な関数が多く含まれているため、データ分析には欠かせません。

import pandas as pd

Seriesの基本的な作成方法

PandasのSeriesは、リストや辞書などのデータから作成できます。単一のデータ列を扱うため、数値や文字列などの1次元データに適しています。

import pandas as pd

# リストからSeriesを作成
data = [10, 20, 30, 40]
series = pd.Series(data)
print(series)

0    10
1    20
2    30
3    40
dtype: int64

カスタムインデックスを設定したSeriesの作成

インデックスをカスタマイズしたい場合は、index引数を使用して任意のインデックスを指定できます。

import pandas as pd

# カスタムインデックスを指定してSeriesを作成
data = [10, 20, 30, 40]
index = ['A', 'B', 'C', 'D']
series = pd.Series(data, index=index)
print(series)
A    10
B    20
C    30
D    40
dtype: int64

辞書からSeriesを作成

PandasのSeriesは辞書からも作成できます。辞書のキーがインデックス、値がデータとして扱われます。

import pandas as pd

# 辞書からSeriesを作成
data = {'A': 10, 'B': 20, 'C': 30, 'D': 40}
series = pd.Series(data)
print(series)
A    10
B    20
C    30
D    40
dtype: int64

データフレームへの変換

複数のSeriesを組み合わせてデータフレームを作成することもできます。以下の例では、2つのSeriesを結合してデータフレームを作成します。

# 複数のSeriesを組み合わせてデータフレームを作成
data1 = pd.Series([10, 20, 30, 40], index=['A', 'B', 'C', 'D'])
data2 = pd.Series(['apple', 'banana', 'cherry', 'date'], index=['A', 'B', 'C', 'D'])

# データフレームに変換
df = pd.DataFrame({'Number': data1, 'Fruit': data2})
print(df)
   Number   Fruit
A      10   apple
B      20  banana
C      30  cherry
D      40    date

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?