Home > python > Python data frequency code

Python data frequency code

This is a very simple script which can be used to get the frequency of any dataset. All you need is a list i.e. the data in a python list format.

So if you have a column of data with repetitions and want to know the frequency of occurrence, you want this. It’s very simple but i wrote it and published it help fasten our daily needs 😀
Here is the function.

def create_frequency_dict(a=[]):
value = 0
for i in range(len(a)):
if i==0:
value += 1
elif a[i] == a[i-1]:
value += 1
elif a[i]!=a[i-1]:
s = str(a[i-1])
b[a[i-1]] = value
value = 1
b[a[i]] = value
return b

  1. July 9, 2010 at 10:21 pm

    A single line bash command, assuming you have the data in a file, one line per datum:

    cat | sort | uniq -c | sort -nr

    • July 9, 2010 at 10:22 pm

      Seems like WordPress ate the “filename” after cat

    • July 9, 2010 at 10:24 pm

      Nice one. I guess i will spend some time with bash 🙂
      Even if I want a python dict, your way is faster if i run the bash script from within python.

  2. Jugantor
    July 19, 2010 at 1:33 am

    very nice, both of you 🙂

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: