我試圖用 google colab 在線解決一個問題。這是代碼單元格。
def add_paths(df, feature_dir, label_dir=None, bands=BANDS):
for band in bands:
df[f"{band}_path"] = feature_dir / df["chip_id"] / f"{band}.tif"
assert df[f"{band}_path"].path.exists().all()
if label_dir is not None:
df["label_path"] = label_dir / (df["chip_id"] ".tif")
assert df["label_path"].path.exists().all()
return df
train_meta = add_paths(train_meta, TRAIN_FEATURES, TRAIN_LABELS)
train_meta.head()
這是我得到的錯誤,
TypeError Traceback (most recent call last)
<ipython-input-46-db866ed40c79> in <module>()
16
17
---> 18 train_meta = add_paths(train_meta, TRAIN_FEATURES, TRAIN_LABELS)
19 train_meta.head()
3 frames
/usr/lib/python3.7/pathlib.py in _parse_args(cls, args)
656 parts = a._parts
657 else:
--> 658 a = os.fspath(a)
659 if isinstance(a, str):
660 # Force-cast str subclasses to str (issue #21127)
TypeError: expected str, bytes or os.PathLike object, not Series
有什么簡單的解決辦法嗎?
uj5u.com熱心網友回復:
您正在嘗試將 Pandas 系列與 pathlib.Path 連接功能相結合,這不是為此目的而設計的(因此出現錯誤訊息“TypeError: expected [...], not Series ”)
對此的解決方案是在嘗試將其組合成路徑之前從資料幀中提取相關單元格。我不知道“band”和“chip_id”是否連接,所以讓我給你一個例子,你只是迭代所有chip_ids(這可能不是你想要的,但應該擺脫錯誤)
def add_paths(df, feature_dir, label_dir=None, bands=BANDS):
for band in bands:
band_paths = []
for chip_id in df.chip_id:
current_path = feature_dir / chip_id / f"{band}.tif"
band_paths.append(current_path)
assert current_path.is_file()
df[f"{band}_path"] = band_paths
if label_dir is not None:
label_paths = []
for chip_id in df.chip_id:
current_path = label_dir / (chip_id ".tif")
assert current_path.is_file()
df["label_path"] = label_paths
return df
train_meta = add_paths(train_meta, TRAIN_FEATURES, TRAIN_LABELS)
train_meta.head()
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/381295.html
上一篇:如何根據另一列中的值創建一列,這些值是我要填充newcol資料的資料框中變數的名稱?電阻
下一篇:PandasDataframesetCategories-不推薦使用pandas.Categorical.set_categories中的`inplace`引數
