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初心者】DataFrameの連結と統計量の取得まとめ

Posted at

Pandasの学習の記録として、DataFrameの連結(concat)と、統計量を取得するための代表的な関数についてまとめておきます。

✅ DataFrameの連結(concat)

複数のDataFrameを結合したい場合は pd.concat() を使います。
行方向(縦)や列方向(横)に連結することができます。

import pandas as pd

df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
df2 = pd.DataFrame({'A': [5, 6], 'B': [7, 8]})

# 行方向(縦方向)に連結
result = pd.concat([df1, df2])
print(result)
   A  B  
0  1  3  
1  2  4  
0  5  7  
1  6  8  
# 列方向(横方向)に連結
result = pd.concat([df1, df2], axis=1)
print(result)
   A  B  A  B
0  1  3  5  7
1  2  4  6  8

✅ 基本的な統計量の取得

以下の関数を使うことで、簡単に統計量を取得できます。

max(最大値)

df.max()

min(最小値)

df.min()

mean(平均値)

df.mean()

median(中央値)

df.median()

mode(最頻値)

df.mode()

mode() は複数行返ることがあるため、最初の行だけ使うには iloc[0, :] を使います。

df.mode().iloc[0, :]

std(標準偏差)

df.std()

count(非欠損値の数)

df.count()

✅ describe(代表的な統計量をまとめて取得)

describe() を使うと、平均・標準偏差・最大値・最小値などが一度に確認できます。

df.describe()
              A         B
count  4.000000  4.000000
mean   3.500000  5.500000
std    2.645751  2.645751
min    1.000000  3.000000
25%    2.250000  4.250000
50%    3.500000  5.500000
75%    4.750000  6.750000
max    6.000000  8.000000

✅ corr(相関係数の計算)

2つのカラムの間にどれくらい関係があるか(相関)を調べるときに使います。

df.corr()
          A         B
A  1.000000  1.000000
B  1.000000  1.000000

1.0 に近いほど強い相関があることを意味します。

✅ drop(列や行の削除)

# 'A'列を削除
df.drop('A', axis=1)
# インデックス0の行を削除
df.drop(0, axis=0)

学習の記録として、今回はDataFrameの連結と統計量の取得について整理しました。
今後も基本的な操作を少しずつまとめていきたいと思います。

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?