如果某一列为空,我想覆盖数据框中的 x 行,并且只覆盖这些单行。我下面的尝试覆盖了所有记录,似乎不仅仅是搜索中返回的记录
表1
>>> route_data
circuit_id circuit_provider circuit_type down errors route site site_id mask next_hop
0 None BOB MPLS False None 10.10.94.0 HORSE 7 255.255.255.0 172.10.1.25
1 None BOB MPLS False None 10.10.82.0 LONDON 8 255.255.255.0 172.10.1.25
2 None BILL MPLS False None 10.10.25.0 BACON 128 255.255.255.0 172.1.1.21
3 None BILL MPLS False None 10.11.0.0 MANC 1 255.255.0.0 NaN
4 None BOB MPLS False None 10.10.66.0 YORK 9 255.255.255.0 172.10.1.25
5 None BOB MPLS False None 10.10.87.0 LIVER 10 255.255.255.0 172.10.1.25
6 None BOB MPLS False None 10.10.120.0 EGGS 11 255.255.255.0 172.10.1.25
表2
>>> device_route_data
circuit_id circuit_provider circuit_type down errors route site site_id mask next_hop
0 None BOB MPLS False None 10.10.94.0 HORSE 7 255.255.255.0 172.17.5.1
1 None BOB MPLS False None 10.10.82.0 LONDON 8 255.255.255.0 172.17.5.1
2 None BILL MPLS False None 10.10.25.0 BACON 128 255.255.255.0 172.16.30.10
3 None BILL MPLS False None 10.11.0.0 MANC 1 255.255.0.0 172.16.30.10
4 None BOB MPLS False None 10.10.66.0 YORK 9 255.255.255.0 172.17.5.1
5 None BOB MPLS False None 10.10.87.0 LIVER 10 255.255.255.0 172.17.5.1
6 None BOB MPLS False None 10.10.120.0 EGGS 11 255.255.255.0 172.17.5.1
获取所有 nan 条目并覆盖
route_data.loc[route_data.next_hop.str.match('nan'), route_data.columns] = device_route_data[device_route_data.columns]
仅返回完整的表2数据
circuit_id circuit_provider circuit_type down errors route site site_id mask next_hop
0 None BOB MPLS False None 10.10.94.0 HORSE 7 255.255.255.0 172.17.5.1
1 None BOB MPLS False None 10.10.82.0 LONDON 8 255.255.255.0 172.17.5.1
2 None BILL MPLS False None 10.10.25.0 BACON 128 255.255.255.0 172.16.30.10
3 None BILL MPLS False None 10.11.0.0 MANC 1 255.255.0.0 172.16.30.10
4 None BOB MPLS False None 10.10.66.0 YORK 9 255.255.255.0 172.17.5.1
5 None BOB MPLS False None 10.10.87.0 LIVER 10 255.255.255.0 172.17.5.1
6 None BOB MPLS False None 10.10.120.0 EGGS 11 255.255.255.0 172.17.5.1
使用
route_data.loc[route_data.next_hop.str.match('nan'), route_data.columns]
成功获取Nan记录
circuit_id circuit_provider circuit_type down errors route site site_id mask next_hop
3 None BILL MPLS False None 10.11.0.0 MANC 1 255.255.0.0 NaN
这是我想重写的唯一记录,我希望所有其他记录保持原样,有人知道我错过了什么吗?
谢谢
编辑:
我试图在 Circuit_type 上做同样的事情,但我得到空白结果。 .isnull() 检查是否也适用于 None 类型?
示例:
circuit_id circuit_provider circuit_type down errors route site site_id mask next_hop
0 None BOB MPLS False None 10.10.94.0 HORSE 7 255.255.255.0 172.17.5.1
1 None BOB MPLS False None 10.10.82.0 LONDON 8 255.255.255.0 172.17.5.1
2 None BILL MPLS False None 10.10.25.0 BACON 128 255.255.255.0 172.16.30.10
3 None BILL MPLS False None 10.11.0.0 MANC 1 255.255.0.0 172.16.30.10
4 None BOB MPLS False None 10.10.66.0 YORK 9 255.255.255.0 172.17.5.1
5 None None None False None 10.10.87.0 LIVER 10 255.255.255.0 172.17.5.1
6 None BOB MPLS False None 10.10.120.0 EGGS 11 255.255.255.0 172.17.5.1
输出
>>> route_data.loc[route_data.circuit_type.isnull(), :]
Empty DataFrame
Columns: [circuit_id, circuit_provider, circuit_type, down, errors, route, site, site_id, mask, next_hop]
Index: []
>>>
编辑2: 进一步测试,这适用于查找行
route_data.loc[route_data.circuit_type.str.contains("None"), :]
但是,当我尝试测试并仅将电路类型覆盖为无的行时,如下所示,它只会覆盖所有行,因此,我只需使用以下命令获取新表,而不是更改“无”行的原始表没有原始数据
f = route_data.loc[route_data.circuit_type.str.contains("None"), :] = device_route_data
最佳答案
您尝试选择的 next_hop
值不是字符串 "nan"
,而是一个称为“非数字”或 NaN
(注意混合大小写)。 Pandas 有方便的函数来处理 NaN 和其他空值,例如 isnull()
:
df1.loc[df.some_column.isnull(), :] = df2
写上你的名字:
route_data.loc[route_data.next_hop.isnull(), :] = device_route_data
isnull()
隔离您感兴趣的列中具有 NaN
值的行。由于您使用的是 .loc[]
,因此您可以使用 :
选择所有列,而无需手动指定它们。而且您也不需要从第二个数据框中选择所有列 - 默认情况下它将使用所有列。
此问题和答案与此类似:Pandas replace all items in a row with NaN if one value is NaN .
关于Python Pandas - 有条件地覆盖另一个数据框中的 x 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52764551/