【发布时间】:2019-06-06 17:16:09
【问题描述】:
我正在搜索 excel 格式的配置转储。
我正在寻找 IP 地址匹配项,我可以在其中收集新的数据帧或匹配的行列表。
到目前为止,我已经成功地迭代了 IP 地址的两列,当它们匹配时查找行并分配给一个变量。
现在我被卡住了,因为使用 pandas 数据框修正方法修正的变量一似乎不起作用。
然后我尝试附加到常规列表,只是这次我知道得到的信息比我需要的多:
68 0 名称:下一个参考索引,dtype:int64
我希望得到一些帮助,试图弄清楚如何摆脱多余的数据并只保留这些值,或者创建一个包含所有匹配项的新数据框以供进一步处理。
# iterate through the two lists of ips and look for
# duplicate values, if value is nan then skip
for line_1 in df_1['IPv4 address']:
# ignore null lines
if 'nan' in str(line_1):
pass
for line_2 in df_2['IPv4 address']:
if 'nan' in str(line_2):
pass
# if the ip addresses match
if line_1 == line_2:
# look up the row in the dataframes ready for comparison
result_1 = df_1.loc[df_1['IPv4 address'] == line_1]
result_2 = df_2.loc[df_2['IPv4 address'] == line_2]
# should I append the different details to a list?
# or append to a dataframe?
# when appending to a dataframe it did not seem to work
# and returned an empty dataframe
result = (str(result_1['IP route name'])
+ str(result_1['IPv4 address'])
+ str(result_1['Next reference index']))
results.append(result)
我希望输出是一个包含所有匹配项的新数据帧,或者是一个列表,每行都是 3 个值
# Example df
【问题讨论】:
-
你好,你能举个df_1和df_2的例子吗?
-
你能看到我附在上面的图片吗?少量提取物。
-
好的,你已经通过拆分df创建了df_1和df_2?
-
不,图像是数据及其标题的示例,Excel 电子表格中有两张表。所以 df_1 是表 1,df_2 是表 2。
标签: python-3.x pandas list dataframe