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?

PandasのDataFrameの初期化や結合

Posted at

はじめに

PandasのDataFrameのひな形や、DataFrame間の結合などの基本をまとめる。

目次

空のDataFrameを作る

pandas_initial_sample.py
import pandas as pd

#空のデータフレーム
df = pd.DataFrame()
print(f'{df=}')

# df=Empty DataFrame
# Columns: []
# Index: []

戻る

DataFrameに列を追加する

pandas_initial_sample.py
import pandas as pd

#列を追加する
df = pd.DataFrame()

#NaNの列追加--------
df['A'] = []*5 #列名Aで空を5行追加
print(f'{df=}')

# df=Empty DataFrame
# Columns: [A]
# Index: []

#0の列追加----------
df['B'] = [0]*5 #列名Bで0を5行追加
print(f'{df=}')

# df=    A  B
# 0 NaN  0
# 1 NaN  0
# 2 NaN  0
# 3 NaN  0
# 4 NaN  0

戻る

DataFrameを任意の値で初期化

辞書型で値を入力すれば、DataFrameを初期化できる。

pandas_initial_sample.py
import pandas as pd

# 空初期化--------------------
df = pd.DataFrame(
	{'A':[]*5
	,'B':[]*5
	,'C':[]*5})
print(f'{df=}')

# df=Empty DataFrame
# Columns: [A, B, C]
# Index: []

# 0初期化---------------------
df = pd.DataFrame(
	{'A':[0]*5
	,'B':[0]*5
	,'C':[0]*5})
print(f'{df=}')

# df=   A  B  C
# 0  0  0  0
# 1  0  0  0
# 2  0  0  0
# 3  0  0  0
# 4  0  0  0

# 任意の値で初期化--------------
df = pd.DataFrame(
	{'a':[1,2,3,4,5]
	,'b':[5,4,3,2,1]})
print(f'{df=}')

# df=   a  b
# 0  1  5
# 1  2  4
# 2  3  3
# 3  4  2
# 4  5  1

戻る

DataFrame間を結合する

pd.concat()を使えば結合ができる。パラメータで結合方法を指定できる。

パラメータ 内容
axis 列結合 axis=1
行結合 axis=0
ignore_index 行結合の場合、indexを採番しなおす
ignore_index=True
  • 列を結合する
pandas_initial_sample.py
import pandas as pd

df1 = pd.DataFrame(
	{'A':[0]*5
	,'B':[0]*5
	,'C':[0]*5})

df2 = pd.DataFrame(
	{'a':[1,2,3,4,5]
	,'b':[5,4,3,2,1]})

# axis=1 で列結合
df = pd.concat([df1,df2],axis=1)
print(f'{df=}')

# df=   A  B  C  a  b
# 0  0  0  0  1  5
# 1  0  0  0  2  4
# 2  0  0  0  3  3
# 3  0  0  0  4  2
# 4  0  0  0  5  1

  • 行を結合する
pandas_initial_sample.py
import pandas as pd

df1 = pd.DataFrame(
	{'A':[0]*5
	,'B':[0]*5})

df2 = pd.DataFrame(
	{'A':[1,2,3,4,5]
	,'B':[5,4,3,2,1]})
	
# axis=1 で行結合
df = pd.concat([df1,df2],axis=0)
print(f'{df=}')

# df=   A  B
# 0  0  0
# 1  0  0
# 2  0  0
# 3  0  0
# 4  0  0
# 0  1  5
# 1  2  4
# 2  3  3
# 3  4  2
# 4  5  1

# axis=1 で行結合 ignore_index=True でindexを採番しなおす
df = pd.concat([df1,df2],axis=0,ignore_index=True)

# df=   A  B
# 0  0  0
# 1  0  0
# 2  0  0
# 3  0  0
# 4  0  0
# 5  1  5
# 6  2  4
# 7  3  3
# 8  4  2
# 9  5  1

戻る

DataFrameから特定の列を抜き出す

列名を指定して抜き出す

pandas_initial_sample.py
import pandas as pd

df = pd.DataFrame(
	{'A':[0]*5
	,'B':[1]*5
	,'C':[2]*5})

# A,C列抜き出し
df_choice = df[['A', 'C']]
print(f'{df_choice=}')

# df_choice=   A  C
# 0  0  2
# 1  0  2
# 2  0  2
# 3  0  2
# 4  0  2

戻る

欠損値NaNの初期化

DataFrame結合時に行列サイズが異なると、欠損値NaNが発生する。欠損値はfillna()で一括変換できる。

pandas_initial_sample.py
import pandas as pd

df1 = pd.DataFrame(
	{'A':[0]*3
	,'B':[0]*3})

df2 = pd.DataFrame(
	{'C':[1]*5
	,'D':[1]*5})

df = pd.concat([df1,df2],axis=1)
print(f'{df=}')

# df=     A    B  C  D
# 0  0.0  0.0  1  1
# 1  0.0  0.0  1  1
# 2  0.0  0.0  1  1
# 3  NaN  NaN  1  1
# 4  NaN  NaN  1  1

# NaN→0変換
df_fillna=df.fillna(0)
print(f'{df_fillna=}')

# df_fillna=     A    B  C  D
# 0  0.0  0.0  1  1
# 1  0.0  0.0  1  1
# 2  0.0  0.0  1  1
# 3  0.0  0.0  1  1
# 4  0.0  0.0  1  1

# NaN→'a'変換
df_fillna=df.fillna('a')
print(f'{df_fillna=}')

# df_fillna=     A    B  C  D
# 0  0.0  0.0  1  1
# 1  0.0  0.0  1  1
# 2  0.0  0.0  1  1
# 3    a    a  1  1
# 4    a    a  1  1

戻る

PandasのExcelCSV処理

戻る

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?