pandas: Dataframe
複数条件:
&,|
否定:
~(NOT)
型変換
検索
選択肢: .isin(list)
result = df[df["code"].isin(["0", "1"])] # 0,1 のみ
result = df[~(df["code"].isin(["0", "1"]))] # 0, 1以外
ユニーク
unique:
items = df["code"].unique()
重複を除外(drop_duplicates):
result = df[["code", "name"]].drop_duplicates()
重複を抽出:
size = df.groupby(keys).size()
res = size[ size > 1 ]
if res.shape[0] > 0:
print(res)
変換
dict 一覧:
dict_list = df.to_dict(orient="records")
GROUP BY
dropna=False 指定しないと、キーの値が NaN(None) の場合、対象にならない
df.groupby(keys, dropna=False)
結果を DataFrame にするにはインデックスをリセットする
df = df.groupby(keys, dropna=False)["amount"].aggregate("sum").reset_index()
nan 行の削除
1つでも nan が含まれていたら:
df = df.dropna()
指定カラムに nan が含まれていたら削除:
df = df.dropna(subset=["amount"])
nan を None に変更
import numpy as np
excel = pd.read_excel(...)
excel = excel.replace([np.nan], [None])
値の変更
def customer_names(row):
return Customer.objects.filter(code=row["code]).values_list("first_name", "last_name").first()
df[['first_name', 'last_name']] = df.apply(customer_names, axis=1, reduce=False)
差分確認( indicator=True)
meged = pd.merge(df_left, df_right, on=["key1","key2","key3"], how='outer',indicator=True)
_merge | 意味 |
|---|---|
both | 一致 |
left_only | df_left のみ存在 |
right_only | df_right のみ存在 |
数値型への変換
# value_str 列の値を数値に変えられるものは変えた列を作る
df["value_num"] = pd.to_numeric(df["value_str"], errors="coerce")
四捨五入
roundは 五捨五超入(偶数丸め)
当月売上w粗利を 1000 円単位で表示
df_epm["sales_profit"] = ((df_epm["当月売上粗利"].str.replace(',', '').astype(int) / 1000) + 0.01).round(0)
openpyxl
wb = self.excel_from_response(response)
ws = wb.worksheets[0]
data = ws.values
columns = next(data)[0:] # 1行目ヘッダー
df = pd.DataFrame(ws.values, columns=columns)
df.drop(df.index[[0]])
列の削除
3 カラム削除:
mg.drop(["created_at", "updated_at", "md5"], axis=1)
Excel
data = pd.read_excel(
"/Users/hdknr/Downloads/処理変更案.xlsx",
sheet_name="名寄一覧表",
skiprows=[0], # 1行目をスキップ
)