python - 在 pandas DataFrame 中查找缺失的数据
问题描述
我正在尝试根据列表中的数据找到一种方法来查找数据框中丢失的数据。每个接口必须有这 5 个子接口。
sub_interface_list = ['1030', '1035', '1039', '1050', '1059']
df = pd.DataFrame({'Device': ['DeviceA', 'DeviceA', 'DeviceA', 'DeviceA', 'DeviceA', 'DeviceA', 'DeviceA', 'DeviceA', 'DeviceA'], 'Interface': ['Eth-Trunk100', 'Eth-Trunk100', 'Eth-Trunk100', 'Eth-Trunk100', 'Eth-Trunk100', 'Eth-Trunk101', 'Eth-Trunk101', 'Eth-Trunk101', 'Eth-Trunk101'], 'Sub_interface': ['1030', '1035', '1039', '1050', '1059', '1030', '1039', '1050', '1059']})
数据框看起来像这样
Device Interface Sub_interface
DeviceA Eth-Trunk100 1030
DeviceA Eth-Trunk100 1035
DeviceA Eth-Trunk100 1039
DeviceA Eth-Trunk100 1050
DeviceA Eth-Trunk100 1059
DeviceA Eth-Trunk101 1030
DeviceA Eth-Trunk101 1039
DeviceA Eth-Trunk101 1050
DeviceA Eth-Trunk101 1059
从列表中我们可以看到 Eth-Trunk101 缺少 1035 的 sub_interface,我想将 1035 插入每个接口的最后一行。我知道使用 dataframe.iterrows() 并搜索丢失的元素很容易,但是 Pandas 中有什么方法可以在不使用 for 循环的情况下使用?
** 这是一个测试数据集,我的数据要大得多,使用迭代会非常耗时。
解决方案
您可以使用pyjanitor的完整功能来公开缺失值:
df.complete(['Interface', 'Sub_interface'])
Interface Sub_interface Device
0 Eth-Trunk100 1030 DeviceA
1 Eth-Trunk100 1035 DeviceA
2 Eth-Trunk100 1039 DeviceA
3 Eth-Trunk100 1050 DeviceA
4 Eth-Trunk100 1059 DeviceA
5 Eth-Trunk101 1030 DeviceA
6 Eth-Trunk101 1035 NaN
7 Eth-Trunk101 1039 DeviceA
8 Eth-Trunk101 1050 DeviceA
9 Eth-Trunk101 1059 DeviceA
您可以使用ffill
填充空值:
df.complete(['Interface', 'Sub_interface']).ffill()
如果你只想坚持在 Pandas 中(pyjanitor 是 Pandas 的方便包装器的集合),下面的解决方案效果很好:
创建interface
和的唯一索引sub_interface
:
interface = pd.MultiIndex.from_product([df.Interface.unique(),
df.Sub_interface.unique()])
In [456]: interface
Out[456]:
MultiIndex([('Eth-Trunk100', '1030'),
('Eth-Trunk100', '1035'),
('Eth-Trunk100', '1039'),
('Eth-Trunk100', '1050'),
('Eth-Trunk100', '1059'),
('Eth-Trunk101', '1030'),
('Eth-Trunk101', '1035'),
('Eth-Trunk101', '1039'),
('Eth-Trunk101', '1050'),
('Eth-Trunk101', '1059')],
)
设置interface
和sub_interface
作为索引,用interface
和reset_index重新索引:
df.set_index(['Interface', 'Sub_interface']).reindex(interface).reset_index()
Interface Sub_interface Device
0 Eth-Trunk100 1030 DeviceA
1 Eth-Trunk100 1035 DeviceA
2 Eth-Trunk100 1039 DeviceA
3 Eth-Trunk100 1050 DeviceA
4 Eth-Trunk100 1059 DeviceA
5 Eth-Trunk101 1030 DeviceA
6 Eth-Trunk101 1035 NaN
7 Eth-Trunk101 1039 DeviceA
8 Eth-Trunk101 1050 DeviceA
9 Eth-Trunk101 1059 DeviceA
在这里重新索引有效,因为 和 的组合interface
是sub_interface
独一无二的;如果它不是唯一的,那么合并outer
是一个更好的步骤;complete在后台处理这些检查。
还要小心使用空值设置索引;Pandas 文档建议避免使用它——尽管到目前为止,我没有发现任何问题。
您也可以使用 unstack/stack,因为索引是唯一的:
df.set_index(['Interface', 'Sub_interface']).unstack().stack(dropna = False).reset_index()
Interface Sub_interface Device
0 Eth-Trunk100 1030 DeviceA
1 Eth-Trunk100 1035 DeviceA
2 Eth-Trunk100 1039 DeviceA
3 Eth-Trunk100 1050 DeviceA
4 Eth-Trunk100 1059 DeviceA
5 Eth-Trunk101 1030 DeviceA
6 Eth-Trunk101 1035 NaN
7 Eth-Trunk101 1039 DeviceA
8 Eth-Trunk101 1050 DeviceA
9 Eth-Trunk101 1059 DeviceA
推荐阅读
- powershell - Active Directory 租户中的 ServicePrincipalNotFound *** - 从 Azure DevOps 在 Powershell 中传递安全变量
- javascript - 我可以在 while 循环中使用 2 个 if 语句吗?
- java - Java Swing - 从处理程序类重绘
- h.264 - 为什么 h264 over rtp 不包含 NALU 起始码
- git - Git没有推动一切
- amazon-web-services - 监控子网带宽
- grails - 旧版本 Grails 和 Apache-Ivy 和 Ant
- node.js - 无服务器调度,检测 cron
- php - 找出创建类实例的目录?
- ios - 如何在 SwiftUI List/ForEach 中动态创建部分并避免“无法推断复杂的闭包返回类型”