我有以下数据集。我试图弄清楚如何通过“未命名9”删除“未命名5”列。我已经尝试了以下代码,但无法正常工作。
数据(名为mapping_table):
Gender|Age|Weight|Height|Unnamed 5| Unnamed 6| Unnamed 7| Unnamed 8| Unnamed 9|
代码:
mapping_table.drop(mapping_table.index[(mapping_table.columns.get_loc("Unnamed: 11")):(mapping_table.columns.get_loc("Unnamed: 19"))],axis=1,inplace=True)
错误:
KeyError: 'labels [11 12 13 14 15 16 17 18] not contained in axis'
有更简单的方法吗?我觉得我应该可以像这样 mapping_table.drop([[mapping.table [“未命名5”:“未命名9”]])
答案 0 :(得分:0)
数据:
df = pd.DataFrame(None, columns = "Gender|Age|Weight|Height".split('|')+['Unnamed ' + str(x) for x in range(0,15)])
Empty DataFrame
Columns: [Gender, Age, Weight, Height, Unnamed 0, Unnamed 1, Unnamed 2, Unnamed 3, Unnamed 4, Unnamed 10, Unnamed 11, Unnamed 12, Unnamed 13, Unnamed 14]
Index: []
代码:
df = df.drop(df.columns[df.columns.str.contains('Unnamed\s[5-9]')], axis = 1)
结果:
Empty DataFrame
Columns: [Gender, Age, Weight, Height, Unnamed 0, Unnamed 1, Unnamed 2, Unnamed 3, Unnamed 4, Unnamed 10, Unnamed 11, Unnamed 12, Unnamed 13, Unnamed 14]
Index: []