'Compare 2 CSV files based on a column and then merge them
I have 2 CSV files and I would like to merge them together.
The second CSV should be integrated into the first, so to speak, so both IP columns should be compared and the ports should then be copied into the first CSV for the correct IP.
I would be very happy if someone could help me :)
I am able to use any Python module
If it's necessary, it would be creating a third file to which everything is exported.
First CSV:
| IP | System | Contact | DNS | Ports |
|---|---|---|---|---|
| 192.168.2.1 | Gateway | xns | gati | |
| 192.168.2.2 | VM1 | etd | vm1etd | |
| 192.168.2.3 | VM2 | rtf | vm2rtf |
As you can see the ports column is still empty because they are saved in the second CSV looking like this:
| IP | Ports |
|---|---|
| 192.168.2.1 | 80, 443, 2037 |
| 192.168.2.2 | 30, 70, 99 |
| 192.168.2.3 | 1020, 1070, 3020 |
Finally it should look like this:
| IP | System | Contact | DNS | Ports |
|---|---|---|---|---|
| 192.168.2.1 | Gateway | xns | gati | 80, 443, 2037 |
| 192.168.2.2 | VM1 | etd | vm1etd | 30, 70, 99 |
| 192.168.2.3 | VM2 | rtf | vm2rtf | 1020, 1070, 3020 |
Solution 1:[1]
What you need simply is merge:
mergedDf = df1.merge(df2, on="IP")
mergedDf
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Amirhossein Kiani |
