我正在嘗試從包含多個以“shape_”開頭的類似列的現有資料集創建一個新的資料框物件。我正在嘗試使用通配符 (*),但出現錯誤。
new_df = df[['date', 'lat', 'long', 'shape_*']]
從我看到的所有示例中,這應該可以作業,但是我收到了錯誤:
KeyError:“['shape_*'] 不在索引中”
uj5u.com熱心網友回復:
使用filter:
cols = list(df.filter(like='shape_').columns)
new_df = df[['date', 'lat', 'long'] cols]
或使用正則運算式:
cols = list(df.filter(regex='^shape_.*').columns)
new_df = df[['date', 'lat', 'long'] cols]
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/451582.html
上一篇:如何計算折扣的運費
