python - 两个数据框,一个比另一个具有更多的列 -> 减去并合并

标签 python csv pandas python-3.5

好吧,我知道标题可能有点令人困惑,但我会尝试详细解释这一点:

我使用Python 3.5.2:

我得到了两个 .csv 文件,我通过 pandas 读取这些文件并将其转换为两个单独的数据帧。第一个数据帧(来自 XYZ.csv)如下所示:

ip              community
10.0.0.1        OL123
.
.
.
123.12.5.31    IK753

第二个 (export.csv) 只有“ip”列。

现在我想做的是:

我想比较两个数据帧,结果得到第三个数据帧(或列表),其中包含第一个数据帧中但不在另一个数据帧中的所有 IP 地址及其相关社区。到目前为止,只要第二个数据帧也包含社区,我就设法比较两者并获得正确的结果。我手动将这些社区插入到第二个 export.csv 中,不幸的是我无法自动执行此操作,这就是为什么我需要它在没有包含社区的第二个数据帧的情况下工作。

这是我的代码:

def compare_csvs():
         timestamp = time.strftime("%Y-%m-%d")

    # Reads XYZ.csv and creates list that contains all ip addresses in integer format.
         A = pd.read_csv("XYZ.csv", index_col=False, header=0)
         ips1 = A.ip.tolist()
         comu1 = A.ro_community.tolist()
         AIP = []
         for element1 in ips1:
                  AIP.append(int(ipaddress.IPv4Address(element1)))
         IPACOM1 = zip(AIP,comu1)              

    # Reads export.csv and creates list that contains all ip addresses in integer format.
         B = pd.read_csv("export" + timestamp + ".csv", index_col=False, header=0)
         ips2 = B.ip.tolist()
         comu2 = B.ro_community.tolist()
         BIP = []
         for element2 in ips2:
                  BIP.append(int(ipaddress.IPv4Address(element2)))
         IPACOM2 = zip(BIP,comu2)

    # Creates a set that contains all ip addresses (in integer format) that exist inside the XYZ.csv but not the export.csv.
         DeltaInt = OrderedSet(IPACOM1)-OrderedSet(IPACOM2)
         List = list(DeltaInt)
         UnzippedIP = []
         UnzippedCommunity = []
         UnzippedIP, UnzippedCommunity = zip(*List)

    # Puts all the elements of the DeltaInt set inside a list and also changes the integers back to readable IPv4-addresses.
         DeltaIP = []
         for element3 in UnzippedIP:
              DeltaIP.append(str(ipaddress.IPv4Address(element3)))

         IPandCommunity = zip(DeltaIP,UnzippedCommunity)

现在我需要的只是可以比较我创建的两个列表并保留“社区”及其分配的“ip”。我尝试了很多,但似乎没有任何效果。也许我只是在这里的逻辑有问题,感谢所有帮助!

另外,请原谅代码困惑,我只是将所有这些放在一起,并在代码实际运行后清理它。

最佳答案

这里有一些可以使用的虚拟数据:

这是 df:

ip              community
10.0.0.1        OL123
10.1.1.1        ACLSH
10.9.8.7        OKUAJ1
123.12.5.31     IK753

df = pd.read_clipboard()

这是导出.csv:

s_export = pd.Series(s_export = pd.Series(name='ip', data=['10.1.1.1','123.12.5.31', '0.0.0.0'])

s_export

0       10.1.1.1
1    123.12.5.31
2        0.0.0.0
Name: ip, dtype: object

要选择未导出的内容,我们可以简单地使用 bool 索引 isin() :

# ~ means 'not', so here that's "find df.ip that is NOT in s_export"
# Store result in a dataframe
df_exclude = df[~df.ip.isin(s_export)]


df_exclude
         ip community
0  10.0.0.1     OL123
2  10.9.8.7    OKUAJ1

关于python - 两个数据框,一个比另一个具有更多的列 -> 减去并合并,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40586590/

相关文章:

python - 为 Microsoft Edge 使用 python selenium

处理多个可能的文件位置的 Pythonic 方式? (不使用嵌套尝试)

python - 使用 Tkinter 用户输入作为函数中的变量

csv 文件上的 Python 正则表达式

java - 在 csv 文件中写入日期,部分输出为 ######

python - 为 mac 制作 pygame/SDL 安装程序

java - Jackson 如何将一个 Pojo 字段映射到 2 个(json)字段(相同的内容,不同的名称)

python - 如何将 pandas 系列写入/读取 csv?

python - 操作值与 HH :MM:SS format - python 不匹配

python - 使用 openweathermAP 将 json 加载到 Pandas 数据帧