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初心者】PandasのDataFrameの計算まとめ

Posted at

PandasのDataFrameを使うと、Excelのように表形式のデータに対して計算処理を行うことができます。
この記事では、学習の記録として、DataFrame同士の演算や、列単位・行単位の演算方法などをまとめておきます。

✅ 使用するサンプルデータ

import pandas as pd

# サンプルデータ作成
df1 = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6]
})

df2 = pd.DataFrame({
    'A': [10, 20, 30],
    'B': [40, 50, 60]
})

print(df1)
   A  B
0  1  4
1  2  5
2  3  6

✅ DataFrame同士の四則演算

print(df1 + df2)    # 加算
    A   B
0  11  44
1  22  55
2  33  66
print(df1 - df2)    # 減算
    A   B
0  -9 -36
1 -18 -45
2 -27 -54
print(df1 * df2)    # 乗算
     A    B
0  10  160
1  40  250
2  90  360
print(df1 / df2)    # 除算
          A         B
0  0.100000  0.100000
1  0.100000  0.100000
2  0.100000  0.100000

✅ 各列に対する演算(スカラーとの演算)

print(df1 + 1)
   A  B
0  2  5
1  3  6
2  4  7
print(df1 * 2)
   A   B
0  2   8
1  4  10
2  6  12

✅ 指定列に対する演算

print(df1['A'] + df1['B'])
0     5
1     7
2     9
dtype: int64
print(df1['A'] * 10)
0    10
1    20
2    30
Name: A, dtype: int64

✅ 行や列の合計・平均など

print(df1.sum())        # 列ごとの合計
A     6
B    15
dtype: int64
print(df1.sum(axis=1))  # 行ごとの合計
0     5
1     7
2     9
dtype: int64
print(df1.mean())       # 列ごとの平均
A    2.0
B    5.0
dtype: float64
print(df1.mean(axis=1)) # 行ごとの平均
0    2.5
1    3.5
2    4.5
dtype: float64

✅ 特定列での集計

print(df1['A'].sum())
6
print(df1['B'].mean())
5.0

✅ 累積計算(cumsum, cumprod)

print(df1.cumsum())
   A   B
0  1   4
1  3   9
2  6  15
print(df1.cumprod())
    A    B
0   1    4
1   2   20
2   6  120

✅ 条件を使った演算

print(df1[df1['A'] > 1])
   A  B
1  2  5
2  3  6
print(df1[df1['A'] > 1]['B'] * 2)
1    10
2    12
Name: B, dtype: int64

✅ 統計系メソッド

print(df1.describe())
              A         B
count  3.000000  3.000000
mean   2.000000  5.000000
std    1.000000  1.000000
min    1.000000  4.000000
25%    1.500000  4.500000
50%    2.000000  5.000000
75%    2.500000  5.500000
max    3.000000  6.000000
print(df1.std())    # 標準偏差
print(df1.var())    # 分散
print(df1.count())  # 非欠損値数
A    1.0
B    1.0
dtype: float64

A    1.0
B    1.0
dtype: float64

A    3
B    3
dtype: int64

✅ 欠損値を含む演算

df_nan = df1.copy()
df_nan.iloc[0, 0] = None
print(df_nan)
     A  B
0  NaN  4
1  2.0  5
2  3.0  6
print(df_nan.sum())                # NaNは無視して計算
print(df_nan.fillna(0).sum())      # NaNを0に置き換えてから計算
A     5.0
B    15.0
dtype: float64

A     5.0
B    15.0
dtype: float64

✅ 新しい列を作って計算結果を保存

df1['A_plus_B'] = df1['A'] + df1['B']
df1['A_times_10'] = df1['A'] * 10
print(df1)
   A  B  A_plus_B  A_times_10
0  1  4         5          10
1  2  5         7          20
2  3  6         9          30

✅ おわりに

PandasのDataFrameでは、Excelのような表形式データに対して簡単に計算を行うことができます。
特に「列全体の加算・平均」や「条件を指定した演算」など、データ分析の基礎に必要な操作を一通り確認できたと思います。

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?