【python】DataFrameやSeriesでの行・列の削除
pandasとnumpyのインポート。
また、pandasからSeriesとDataFrameもインポートする。
import pandas as pd import numpy as np from pandas import Series, DataFrame
シリーズの場合
テスト用のシリーズを作成
series1 = Series(np.arange(5), index=['A','B','C','D','E'])
実行結果
A 0 B 1 D 3 E 4 dtype: int32
DataFrameの場合
テスト用のDataFrameを作成
dataframe1 = DataFrame(np.arange(9).reshape(3,3), index=['A','B','C'], columns=['col1','col2','col3'])
実行結果
col1 col2 col3 B 3 4 5 C 6 7 8
列の削除
dataframe1.drop('col2',axis=1)
列の削除を行う場合は、axis=1を指定する。
実行結果
col1 col3 A 0 2 B 3 5 C 6 8