如何根据 pandas 中的条件删除行,并将字符串转换为整数进行比较

我试图删除“建造年份”列中值小于 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


MMTTMM
浏览 99回答 2
2回答

神不在的星期二

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

有只小跳蛙

当您同时使用 .loc 和布尔索引时,熊猫似乎会变得混乱。一项简单的修改将仅使用布尔索引 dframe_sub = dframe[dframe[13].astype(int) >= 1920]。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python