我有一个数据框,我想通过fip代码进行过滤,而不仅仅是使用状态列。如果我想选择“ fip”列值以53开头的所有行,对于华盛顿州,我该怎么做?我宁愿不将类型转换为str并使用str.startswith。我正在寻找str.startswith的等效项,但要寻找一个整数。我的输入数据帧是第一张图像,我的预期输出数据帧是第二张图像。数据来自ny Times网站,用于跟踪可在github上免费获得的日冕病毒。
答案 0 :(得分:0)
让我们尝试
subdf=df.loc[df.state.eq('Washington') & df.fips.astype(str).str[:2].eq('53'),:].copy()
#adding the copy here try to avoid the copy warning when you modify the selected data frame