如果某列为空,并且只覆盖这些行,我希望覆盖数据帧中的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]成功地获得了南的记录
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"), :]但是,当我尝试测试并覆盖只有电路类型为none的行时,它只是覆盖所有的行,因此,与原始表中没有任何原始数据,而不是原始表中的None行相比,我只得到了新表。
f = route_data.loc[route_data.circuit_type.str.contains("None"), :] = device_route_data发布于 2018-10-11 16:16:34
您要选择的next_hop值不是字符串"nan",而是一个称为"not a number“或NaN (注意混合大写)的特殊值。熊猫有处理NaN和其他空值的方便函数,如isnull()。
df1.loc[df.some_column.isnull(), :] = df2用你们的名字:
route_data.loc[route_data.next_hop.isnull(), :] = device_route_dataisnull()使用感兴趣的列中的NaN值隔离行。因为您使用的是.loc[],所以可以使用:选择所有列,而不必手动指定它们。您也不需要从第二个dataframe中选择所有列--默认情况下它将使用它们。
这个问题和答案类似于这个问题:Pandas replace all items in a row with NaN if one value is NaN。
https://stackoverflow.com/questions/52764551
复制相似问题