Home > other >  How to print contents in columns if contents is same value, just print once. Pandas
How to print contents in columns if contents is same value, just print once. Pandas

Time:10-27

I have dataset like this, I want to print contents in 'Protocol' columns.

pcap_df = pd.read_csv('pcap.csv')
pcap_df.head(10)

    No. Time    Source  Destination Protocol    Length  Info
0   1   0.000000    172.253.118.188 192.168.1.34    TLSv1.2 1334    Application Data
1   2   0.000272    172.253.118.188 192.168.1.34    TLSv1.2 788 Application Data
2   3   0.000309    192.168.1.34    172.253.118.188 TCP 54  43242 > 5228 [ACK] Seq=1 Ack=2015 Win=64400 ...
3   4   0.042950    192.168.1.34    74.125.24.136   QUIC    1292    Initial, DCID=be8c031f7abfaec5, PKN: 1, PING, ...
4   5   0.043241    192.168.1.34    74.125.24.136   QUIC    120 0-RTT, DCID=be8c031f7abfaec5
5   6   0.043469    192.168.1.34    74.125.24.136   QUIC    1288    0-RTT, DCID=be8c031f7abfaec5
6   7   0.043991    192.168.1.34    74.125.24.136   QUIC    169 0-RTT, DCID=be8c031f7abfaec5
7   8   0.053225    192.168.1.34    74.125.130.132  TCP 55  59756 > 443 [ACK] Seq=1 Ack=1 Win=64361 Len=...
8   9   0.065535    74.125.24.136   192.168.1.34    QUIC    1292    Protected Payload (KP0)
9   10  0.065535    74.125.24.136   192.168.1.34    QUIC    824 Protected Payload (KP0)

and if I run

pcap_df['Protocol']

result

0       TLSv1.2
1       TLSv1.2
2           TCP
3          QUIC
4          QUIC
         ...   
8324    TLSv1.3
8325    TLSv1.3
8326        TCP
8327        TCP
8328        TCP

And I want output like this.

TLSv1.2
TCP
QUIC
TLSv1.3

I've been searching for solution within 3 days, but still, not get right answer.

CodePudding user response:

Looks like you want to use df.unique(). That will give an array that contains each unique value. Then a little playing for exactly how you want to display.

pcap_df['Protocol'].unique()

Then you can print it, convert to a list and print, make a loop to print each value on a line, etc etc

  • Related