'Printing specific columns from pandas dataframe based on a condition
My Pandas dataframe looks like this. How do I get the output where the STATE is closed and display the ForeignAddr and ForeignPort
I tried the following and it only gives the IP.
print(dfNetscan[(dfNetscan.State == 'CLOSED')]["ForeignAddr"].values[0])
When I try adding the port asdfNetscan[(dfNetscan.State == 'CLOSED')]["ForeignAddr"]["ForeignPort"].values[0] it errors out.
TreeDepth Offset Proto LocalAddr LocalPort ForeignAddr ForeignPort State PID Owner Created
0 0 0xac818d494050 UDPv4 0.0.0.0 0 * 0 NaN 776 lsass.exe 2021-04-01 05:05:03.000000
1 0 0xac818d494050 UDPv6 :: 0 * 0 NaN 776 lsass.exe 2021-04-01 05:05:03.000000
2 0 0xac818db931a0 TCPv4 0.0.0.0 135 0.0.0.0 0 LISTENING 496 svchost.exe 2021-04-01 05:05:02.000000
3 0 0xac818db931a0 TCPv6 :: 135 :: 0 LISTENING 496 svchost.exe 2021-04-01 05:05:02.000000
4 0 0xac818db932f0 TCPv4 0.0.0.0 49664 0.0.0.0 0 LISTENING 632 wininit.exe 2021-04-01 05:05:02.000000
.. ... ... ... ... ... ... ... ... ... ... ...
74 0 0xac8192d4c910 TCPv4 10.1.1.182 52871 13.75.160.154 443 SYN_SENT 2992 MsMpEng.exe 2021-04-06 01:56:51.000000
75 0 0xac8192f10ba0 TCPv4 10.1.1.182 52868 184.85.82.103 80 CLOSED 652 svchost.exe 2021-04-06 01:56:38.000000
76 0 0xac81949e9930 TCPv4 10.1.1.182 52763 13.54.35.87 5555 ESTABLISHED 32 PSclient.exe 2021-04-06 01:02:26.000000
77 0 0xac81959954a0 TCPv4 10.1.1.182 3389 10.2.0.196 56343 ESTABLISHED 1092 svchost.exe 2021-04-06 01:55:11.000000
78 0 0xac81959ebb80 TCPv4 10.1.1.182 5985 10.1.1.110 63743 ESTABLISHED 4 System 2021-04-06 01:02:25.000000
Solution 1:[1]
dfNetscan[(dfNetscan.State == 'CLOSED')][["ForeignAddr","ForeignPort"]].values[0]
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 | MoRe |
