比较往返列表值并更改它们 Pandas

Compare round-trip list values and change them Pandas

我有一个 df 数据框。数据框具有三列,分别名为“源”、“目标”和“路径”。 Path栏是一个列表,值是各个路由器的路径。 “Source”表示各个路由器路径的起点,“Destination”表示各个路由器路径的终点:

Source      Destination   Path
---------- ------------ -------------------------------------------------
10.0.13.100 10.0.14.100 ['10.0.1.26']
10.0.14.100 10.0.13.100 ['10.0.1.25']
10.0.13.100 10.0.12.100 ['10.0.1.17', '10.0.1.5', '10.0.1.2']
10.0.12.100 10.0.13.100 ['10.0.1.1', '10.0.1.6', '10.0.1.18']
10.0.13.100 10.0.22.100 ['10.0.1.17', '10.0.1.14', '10.0.4.6', '10.0.2.5']
10.0.22.100 10.0.13.100 ['10.0.2.6', '10.0.4.5', '10.0.1.13', '10.0.1.18']
10.0.11.100 10.0.13.100 ['10.0.1.6', '10.0.1.18']
10.0.13.100 10.0.11.100 ['10.0.1.17', '10.0.1.5']
.....................................................................

我想创建一个名为“Path_Def”的附加列,其中 Path 列的 return 路径是参考 Source 和 Destination 列进行评估的。

示例: 我使用路径 [....] 评估原点 -> 10.0.13.100 和目的地 -> 10.0.14.100 并评估其反向路径或原点 -> 10.0.14.100 和目的地 -> 10.0.13.100 及其路径 [.. ..]。 我想做的是通过反转这些值将第二条路径的值更改为第一条路径的值(总是指起点和终点)。

所以你应该为列表中的每个值做这样的事情:

  Source        Destination   Path
  10.0.13.100   10.0.22.100  ['10.0.1.17', '10.0.1.14', '10.0.4.6', '10.0.2.5']
  10.0.22.100   10.0.13.100  ['10.0.2.5', '10.0.4.6', '10.0.1.14', '10.0.1.17']
.............................................................................

..

我希望数据框中的结果是这样的:

Source      Destination   Path_Def                                      
10.0.13.100 10.0.14.100  ['10.0.1.26']                                
10.0.14.100 10.0.13.100  ['10.0.1.26']
10.0.13.100 10.0.12.100  ['10.0.1.17', '10.0.1.5', '10.0.1.2']
10.0.12.100 10.0.13.100  ['10.0.1.2', '10.0.1.5', '10.0.1.17']
10.0.13.100 10.0.22.100  ['10.0.1.17', '10.0.1.14', '10.0.4.6', '10.0.2.5']
10.0.22.100 10.0.13.100  ['10.0.2.5', '10.0.4.6', '10.0.1.14', '10.0.1.17']
10.0.11.100 10.0.13.100  ['10.0.1.6', '10.0.1.18']
10.0.13.100 10.0.11.100  ['10.0.1.18', '10.0.1.6']
.....................................................................

如何做到这一点?我真的不知道从哪里开始,将不胜感激

在@Simon代码后编辑

编辑 合并 DF:

编辑 最后一个代码:

将数据框合并到自身,然后应用条件并最终清理列:

import pandas as pd

# setup
source = ["10.0.14.100", "10.0.13.100", "10.0.12.100", "10.0.13.100", "10.0.22.100", "10.0.11.100", "10.0.13.100", "10.0.13.100"]
destination = ["10.0.13.100", "10.0.12.100", "10.0.13.100", "10.0.22.100", "10.0.13.100", "10.0.13.100", "10.0.11.100", "10.0.14.100"]
path = [['10.0.1.25'], ['10.0.1.17', '10.0.1.5', '10.0.1.2'], 
        ['10.0.1.1', '10.0.1.6', '10.0.1.18'], ['10.0.1.17', '10.0.1.14', '10.0.4.6', '10.0.2.5'], 
        ['10.0.2.6', '10.0.4.5', '10.0.1.13', '10.0.1.18'], ['10.0.1.6', '10.0.1.18'], ['10.0.1.17', '10.0.1.5'], ['10.0.1.26']]

df = pd.DataFrame({"source":source, "destination":destination, "path":path})

# join column onto itself
df = df.merge(df, left_on=["destination", "source"], right_on=["source", "destination"])
# Conditionally choose the right path
df["Path_Def"] = np.where(df["source_x"] < df["destination_x"], df["path_x"], df["path_y"])
# Flip the ones that need to be flipped
df["Path_Def"] = [path[::-1] if source > destination else path for path, source, destination in zip(df["Path_Def"].values, df["source_x"].values, df["destination_x"].values)]


# select columns you want to keep
df = df[["source_x", "destination_x", "Path_Def"]]
print(df)

输出:

      source_x destination_x                                    Path_Def
0  10.0.14.100   10.0.13.100                                 [10.0.1.26]
1  10.0.13.100   10.0.12.100             [10.0.1.18, 10.0.1.6, 10.0.1.1]
2  10.0.12.100   10.0.13.100             [10.0.1.1, 10.0.1.6, 10.0.1.18]
3  10.0.13.100   10.0.22.100  [10.0.1.17, 10.0.1.14, 10.0.4.6, 10.0.2.5]
4  10.0.22.100   10.0.13.100  [10.0.2.5, 10.0.4.6, 10.0.1.14, 10.0.1.17]
5  10.0.11.100   10.0.13.100                       [10.0.1.6, 10.0.1.18]
6  10.0.13.100   10.0.11.100                       [10.0.1.18, 10.0.1.6]
7  10.0.13.100   10.0.14.100                                 [10.0.1.26]