【问题标题】:Python Pandas: How to append a dataframe to another whilst iterating?Python Pandas:如何在迭代时将数据框附加到另一个?
【发布时间】: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

enter image description here

【问题讨论】:

  • 你好,你能举个df_1和df_2的例子吗?
  • 你能看到我附在上面的图片吗?少量提取物。
  • 好的,你已经通过拆分df创建了df_1和df_2?
  • 不,图像是数据及其标题的示例,Excel 电子表格中有两张表。所以 df_1 是表 1,df_2 是表 2。

标签: python-3.x pandas list dataframe


【解决方案1】:

你可以试试这样的: 您在重命名列后加入两个数据框,然后通过执行 pandas 过滤器直接比较列 IPV4,这样您只提取所需的行:

import pandas as pd
import numpy as np

df2 = pd.DataFrame(np.array([["ip_route1", "1.1.1.1",1], ["ip_route2", "2.2.2.2",2],["ip_route2", "4.3.3.4",3]]),columns=['IP_ROUTE', 'IPV4_AD','REF_INDEX'])
df1 = pd.DataFrame(np.array([["ip_route1", "1.1.1.1",1], ["ip_route2", "2.2.2.2",2], ["ip_route2", "3.3.3.3",3]]),columns=['IP_ROUTE', 'IPV4_AD','REF_INDEX'])

print(df1)
#     IP_ROUTE  IPV4_AD REF_INDEX
# 0  ip_route1  1.1.1.1         1
# 1  ip_route2  2.2.2.2         2
# 2  ip_route2  3.3.3.3         3

print(df2)
#     IP_ROUTE  IPV4_AD REF_INDEX
# 0  ip_route1  1.1.1.1         1
# 1  ip_route2  2.2.2.2         2
# 2  ip_route2  4.3.3.4         3

df1 = df1.rename(columns={"IPV4_AD": "IPV4_AD1", "IP_ROUTE":"IP_ROUTE_1"}).set_index("REF_INDEX")
df2 = df2.rename(columns={"IPV4_AD": "IPV4_AD2", "IP_ROUTE":"IP_ROUTE_2"}).set_index("REF_INDEX")

df = df1.join(df2).reset_index()

print(df)
#   REF_INDEX IP_ROUTE_1 IPV4_AD1 IP_ROUTE_2 IPV4_AD2
# 0         1  ip_route1  1.1.1.1  ip_route1  1.1.1.1
# 1         2  ip_route2  2.2.2.2  ip_route2  2.2.2.2
# 2         3  ip_route2  3.3.3.3  ip_route2  4.3.3.4

TempResult = df[df["IPV4_AD1"]==df["IPV4_AD2"]]
print(TempResult)
#   REF_INDEX IP_ROUTE_1 IPV4_AD1 IP_ROUTE_2 IPV4_AD2
# 0         1  ip_route1  1.1.1.1  ip_route1  1.1.1.1
# 1         2  ip_route2  2.2.2.2  ip_route2  2.2.2.2

result = TempResult[["IP_ROUTE_1","IPV4_AD1"]]
# .rename(columns={"IP_ROUTE_1","IP_ROUTE","IPV4_AD1","IPV4_AD"})
print(result)$
#   IP_ROUTE_1 IPV4_AD1
# 0  ip_route1  1.1.1.1
# 1  ip_route2  2.2.2.2

【讨论】:

  • 你可以吗@Henry James?
  • 不,抱歉,这不能回答我的问题,我很害怕!
猜你喜欢
  • 1970-01-01
  • 2019-01-28
  • 1970-01-01
  • 2018-06-06
  • 2021-01-19
  • 1970-01-01
  • 2022-12-11
  • 1970-01-01
  • 2023-01-16
相关资源
最近更新 更多