実現したいこと
エラーを解消する
前提
jupyterlabでpythonを使って、作業用のedaフォルダからまとめなおしたモデルフォルダを作成しています。
発生している問題・エラーメッセージ
KeyError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_27964\3229945832.py in <module>
----> 1 df = data_pre(df)
2 df.info()
~\AppData\Local\Temp\ipykernel_27964\2112981477.py in data_pre(df)
7 df = df.drop(nonnull_list, axis=1)
8
----> 9 df = df.drop("市区町村名", axis=1)
10
11 df = df.drop("種類", axis=1)
~\anaconda3\lib\site-packages\pandas\util_decorators.py in wrapper(*args, **kwargs)
309 stacklevel=stacklevel,
310 )
--> 311 return func(*args, **kwargs)
312
313 return wrapper
~\anaconda3\lib\site-packages\pandas\core\frame.py in drop(self, labels, axis, index, columns, level, inplace, errors)
4955 weight 1.0 0.8
4956 """
-> 4957 return super().drop(
4958 labels=labels,
4959 axis=axis,
~\anaconda3\lib\site-packages\pandas\core\generic.py in drop(self, labels, axis, index, columns, level, inplace, errors)
4265 for axis, labels in axes.items():
4266 if labels is not None:
-> 4267 obj = obj._drop_axis(labels, axis, level=level, errors=errors)
4268
4269 if inplace:
~\anaconda3\lib\site-packages\pandas\core\generic.py in _drop_axis(self, labels, axis, level, errors, consolidate, only_slice)
4309 new_axis = axis.drop(labels, level=level, errors=errors)
4310 else:
-> 4311 new_axis = axis.drop(labels, errors=errors)
4312 indexer = axis.get_indexer(new_axis)
4313
~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in drop(self, labels, errors)
6659 if mask.any():
6660 if errors != "ignore":
-> 6661 raise KeyError(f"{list(labels[mask])} not found in axis")
6662 indexer = indexer[~mask]
6663 return self.delete(indexer)
KeyError: "['市区町村名'] not found in axis"
該当のソースコード
def data_pre(df):
nonnull_list = []
for col in df.columns:
nonnull = df[col].count()
if nonnull == 0:
nonnull_list.append(col)
df = df.drop(nonnull_list, axis=1)
df = df.drop("市区町村名", axis=1) df = df.drop("種類", axis=1) dis = { "30分?60分":45, "1H?1H30":75, "2H?":120, "1H30?2H":105 } df["最寄駅:距離(分)"] = df["最寄駅:距離(分)"].replace(dis).astype(float) df["面積(㎡)"] = df["面積(㎡)"].replace("2000㎡以上", 2000).astype(float) y_list = {} for i in df["建築年"].value_counts().keys(): if "平成" in i: num = float(i.split("平成")[1].split("年")[0]) year = 35 - num if "令和" in i: num = float(i.split("令和")[1].split("年")[0]) year = 5 - num if "昭和" in i: num = float(i.split("昭和")[1].split("年")[0]) year = 98 - num y_list[i] = year y_list["戦前"] = 78 df["建築年"] = df["建築年"].replace(y_list) year = { "年第1四半期":".25", "年第2四半期":".50", "年第3四半期":".75", "年第4四半期":".99" } year_list = {} for i in df["取引時点"].value_counts().keys(): for k, j in year.items(): if k in i: year_rep = i.replace(k, j) year_list[i] = year_rep df["取引時点"] = df["取引時点"].replace(year_list).astype(float) for col in ["都道府県名", "地区名", "最寄駅:名称", "間取り", "建物の構造", "用途", "今後の利用目的", "都市計画", "改装", "取引の事情等"]: df[col] = df[col].astype("category") return df
df = data_pre(df)
df.info()
試したこと
「市区町村名」をコピーして貼りなおしたり、付近に全角スペースが混ざっていないか確認しました。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。

回答1件
あなたの回答
tips
プレビュー