【问题标题】:How to iterate each row of a column from a DataFrame and delete the non-numeric rows如何从 DataFrame 中迭代列的每一行并删除非数字行
【发布时间】: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

【问题讨论】:

标签: python python-3.x dataframe data-science


【解决方案1】:

导入数学 GbCity=new_dataset_2['GbCity'].astype("str").apply(lambda x:x if x.isnumeric() else math.nan) GbCity=GbCity.dropna()

【讨论】:

    猜你喜欢
    • 2021-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-25
    • 2012-09-25
    • 2019-10-24
    • 1970-01-01
    • 2017-11-27
    相关资源
    最近更新 更多