1
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のpandasとは

1
Posted at

🐼 pandasとは?

・pandas(パンダス)は、Pythonで データの読み込み・整形・分析 を簡単に行うためのライブラリです。
・Excelやデータベースのように、表形式(表=データフレーム) のデータを自在に扱えるのが特徴です。

🧩 pandasの主な構造

pandasには主に2つのデータ構造があります

構造名 内容
Series1 1次元 1列のデータ(ラベル付き) [10, 20, 30]
DataFrame 2次元 行と列を持つ表形式のデータ Excelの表のような構造

コード

test.py
import pandas as pd

data = {
    "名前": ["Alice", "Bob", "Charlie"],
    "年齢": [25, 30, 22],
    "出身": ["東京", "大阪", "福岡"],
}

df = pd.DataFrame(data)
print(df)

実行結果

(venv) kerumatomomitsu@kerumatomomitsunoMacBook-Air stock_portfolio % python test.py
        
        名前  年齢  出身
0    Alice  25  東京
1      Bob  30  大阪
2  Charlie  22  福岡

データの基本操作

操作 コード例 説明
行数・列数 df.shape (行, 列) を表示
列を取得 df["年齢"] 特定の列をSeriesとして取得
条件抽出 df[df["年齢"] > 25] 25歳より上の行だけ取得
並べ替え df.sort_values("年齢", ascending=False) 年齢で降順ソート
平均値 df["年齢"].mean() 平均を計算
新しい列追加 df["年齢×2"] = df["年齢"] * 2 計算列を追加
CSV出力 df.to_csv("data.csv", index=False) CSVファイルに保存

コード

pandas_basic_operations.py
import pandas as pd

# === サンプルデータ作成 ===
data = {
    "名前": ["Alice", "Bob", "Charlie", "David", "Eve"],
    "年齢": [25, 30, 22, 28, 35],
    "出身": ["東京", "大阪", "福岡", "札幌", "名古屋"],
}

df = pd.DataFrame(data)

print("=== 元のデータ ===")
print(df)
print()

# === 行数・列数を表示 ===
print("① 行数・列数:")
print(df.shape)  # (行, 列)
print()

# === 特定の列を取得 ===
print("② 列を取得(年齢):")
print(df["年齢"])
print()

# === 条件抽出 ===
print("③ 条件抽出(年齢 > 25):")
print(df[df["年齢"] > 25])
print()

# === 並べ替え ===
print("④ 並べ替え(年齢で降順ソート):")
print(df.sort_values("年齢", ascending=False))
print()

# === 平均値 ===
print("⑤ 年齢の平均値:")
print(df["年齢"].mean())
print()

# === 新しい列の追加 ===
df["年齢×2"] = df["年齢"] * 2
print("⑥ 新しい列の追加(年齢×2):")
print(df)
print()

# === CSV出力 ===
df.to_csv("data.csv", index=False, encoding="utf-8-sig")
print("⑦ CSVファイルを保存しました → data.csv")

実行結果

(venv) kerumatomomitsu@kerumatomomitsunoMacBook-Air stock_portfolio % python pandas_basic_operations.py
=== 元のデータ ===
        名前  年齢   出身
0    Alice  25   東京
1      Bob  30   大阪
2  Charlie  22   福岡
3    David  28   札幌
4      Eve  35  名古屋

① 行数・列数:
(5, 3)

② 列を取得(年齢):
0    25
1    30
2    22
3    28
4    35
Name: 年齢, dtype: int64

③ 条件抽出(年齢 > 25):
      名前  年齢   出身
1    Bob  30   大阪
3  David  28   札幌
4    Eve  35  名古屋

④ 並べ替え(年齢で降順ソート):
        名前  年齢   出身
4      Eve  35  名古屋
1      Bob  30   大阪
3    David  28   札幌
0    Alice  25   東京
2  Charlie  22   福岡

⑤ 年齢の平均値:
28.0

⑥ 新しい列の追加(年齢×2):
        名前  年齢   出身  年齢×2
0    Alice  25   東京    50
1      Bob  30   大阪    60
2  Charlie  22   福岡    44
3    David  28   札幌    56
4      Eve  35  名古屋    70

⑦ CSVファイルを保存しました → data.csv

⚙️ pandasのメリットまとめ

特徴 説明
✅ Excelより高速 数百万行のデータも効率的に処理できる
✅ コードで自動化可能 手作業不要、再現性あり
✅ 強力な統計関数 平均・分散・グループ集計も1行で可能
✅ 他のライブラリと連携 matplotlib, NumPy, scikit-learnなど
1
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
1
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?