3
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Pandas

Posted at
DataFrameの作成
df = pd.DataFrame({'C1':[1, 2, 3],
                   'C2':[4, 5, 6], 
                   'C3':[7, 8, 9]},
                   index = [A, B, C])
ユニークな値の数を確認
df.unique()
欠損値の確認
df.isnull().sum()
行名・列名を確認
df.index
df.columns

データの選択と抽出

基本的な選択
df.loc['A']
df.loc[:, 'C1']
df.loc['A', ['C1', 'C3']]

df.iloc[1]

条件による選択

df > 0  #ブール型
df[df > 0] #Falseの部分がNaNで返ってくる
df['C1'] > 0
df[df['C1'] > 0]
df[(df['C1'] > 0) & (df['C1] < 1)]

データの追加と削除

df['New Column'] = df['C1'] * df['C2']
df.drop(columns=['C1']) #「inplace=True」にすると元のデータを書き換える

欠損値の処理

削除
df.dropna()
df['C1'].dropna()

df.isnull()
df[df['C2'].isnull() == False] #C2が欠損していないところだけを取り出す

df.dropna(thresh=3) #欠損値の数を指定
df.dropna(thresh=3axis=1) #列に適用

置換
df['C2'].fillna(df['C2'].mean())
df.fillna(df.mean())
カテゴリカルなデータ
df['C1'].value_counts() #それぞれのカテゴリの数を返す
df['C1'] = df['C1'].fillna(df['C1'].mode()[0])

round(df['C1'].value_counts / len(df), 2)

df.groupby('C1').sum() #カテゴリごとに合計
df.groupby('C1').mean()
df.groupby('C1').max()

DataFrameの結合

pd.concat([df_1, ds_2])
pd.concat([df_1, ds_2], axis=1, sort=True)
3
7
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
3
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?