【发布时间】:2022-01-17 08:00:56
【问题描述】:
我有此代码,但无法从 DataFrame 中删除特定行。如何改进此代码?我想从该列中删除非数字的值。这种方式不起作用,因为之前删除了一些丢失的列,所以我得到了
KeyError: '[1093] not found in axis'
代码:
i = 0
for row in new_dataset_2.GbCity:
if isinstance(row,str):
if not(row.isdigit()):
new_dataset_2.drop(i)
i+=1
【问题讨论】:
-
No... 它不会删除所有非数字,因为 : for row in new_dataset_2.GbCity: if isinstance(row,str): if not(row.isdigit()): print(row) new_dataset_2.drop(new_dataset_2.index[new_dataset_2["GbCity"]== row]) is 's'
-
您能否添加一个 new_dataset_2 的样本以便能够重现此问题? (见How to make good reproducible pandas examples)。
标签: python python-3.x dataframe data-science