gpt4 book ai didi

python - 在 pandas DataFrame 中查找缺失的数据

转载 作者:行者123 更新时间:2023-12-04 02:27:19 25 4
gpt4 key购买 nike

我正在尝试根据列表中的数据找到一种方法来查找数据框中丢失的数据。每个接口(interface)必须有这5个子接口(interface)。

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插入到每个接口(interface)的最后一行。我知道使用 dataframe.iterrows() 并搜索丢失的元素很容易,但是 pandas 中有没有什么方法可以在不使用 for 循环的情况下使用?

** 这是一个测试数据集,我的数据要大得多,使用迭代会非常耗时。

最佳答案

您可以使用 complete 中的 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 的一系列方便的包装器),下面的解决方案很有效:

创建interfacesub_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')],
)

设置interfacesub_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

这里重新索引是有效的,因为 interfacesub_interface 的组合是唯一的;如果它不是唯一的,那么在 outer 上合并是一个更好的步骤; complete 在后台负责这些检查。

还要小心设置索引为空值; Pandas docs suggests to avoid it - 虽然,到目前为止,我还没有注意到重建索引的任何问题。

你也可以使用 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

关于python - 在 pandas DataFrame 中查找缺失的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66540099/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com