Search code examples
pythonpandasip-addressdata-miningvalueerror

Histogram of IP addresses (Panda Series)


I wish to plot a histogram to check frequency of occurrence of IP addresses used for data mining.

My snippet:-

import pandas as pd
import matplotlib.pyplot as plt

p1 = r'small_set.csv'
d = pd.read_csv(p1, engine='python')
source_ip = d['Source IP']
source_ip.hist()

My 'source_ip' is a panda series type variable, that looks as follows:-

>>> source_ip
0          8.0.69.0
1          8.0.69.0
2          8.0.69.0
3          8.0.69.0
4          8.0.69.0
5          8.0.69.0
          ...      
69    192.168.10.17
70    192.168.10.17
71    192.168.10.17
72    192.168.10.17
73    192.168.10.17
74    192.168.10.17
Name: Source IP, Length: 74, dtype: object

However at line source_ip.hist(), I get the following error:-

File "/home/developer/.local/lib/python2.7/site-packages/numpy/lib/histograms.py", line 253, in _get_outer_edges
    "supplied range of [{}, {}] is not finite".format(first_edge, last_edge))
ValueError: supplied range of [inf, 8.0.69.0] is not finite

As a work-around, I found the count of the frequencies using value_counts() as follows:-

s = d['Source IP'].value_counts()
>>> s
8.0.69.0         28
192.168.10.17    26
192.168.10.12    25
192.168.10.19    12
192.168.10.50     8
Name: Source IP, dtype: int64

But it's still not the same. How do I get rid of that Value Error and display a legit histogram?


Solution

  • You need a qualitative histogram

    df['Source IP'].value_counts().plot(kind='bar')
    

    Other SO question