首页 > 解决方案 > 如何根据熊猫中的条件删除一行,并将字符串转换为整数进行比较

问题描述

我正在尝试删除 Year Built 列中值小于 1920 的所有行。我不明白如何转换为整数,因此我可以评估不到 1920 年的建造条件,然后移除那些在 1920 年之前建造的建筑物。任何方向都值得赞赏!

没有错误:

YearBuilt_convert_to_integer = dframe[13].astype(int)

没有错误:

YearBuilt_less_than_1920 = YearBuilt_convert_to_integer < 1920

输出:

YearBuilt_less_than_1920
42      False
43      False
44      False
45       True
46      False
        ...  
3533    False
3534    False
3535    False
3536    False
3537    False
Name: 13, Length: 3347, dtype: bool

产生 0 行:

dframe = dframe.loc[~dframe[13].astype(int)<1920, :]

    0   1   2   3   4   5   6   7   8   9   ... 32  33  34  35  36  37  38  39  40  41
0 rows × 42 columns

标签: pythonpandasdataframeconditional-statements

解决方案


dframe[np.invert(dframe.loc[:,13].astype(int)<1920)]

推荐阅读