How Can I implement functions like mean.median and variance if I have a dictionary with 2 keys in Python?
<p>I have many files in a folder that like this one:
<a href="http://i.stack.imgur.com/MitYd.png" rel="nofollow">enter image description here</a></p>

<p>and I'm trying to implement a dictionary for data. I'm interested in create it with 2 keys (the first one is the http address and the second is the third field (plugin used), like adblock). The values are referred to different metrics so my intention is to compute the for each site and plugin the mean,median and variance of each metric, once the dictionary has been implemented. For example for the mean, my intention is to consider all the 4-th field values in the file, etc. I tried to write this code but, first of all, I'm not sure that it is correct.
<a href="http://i.stack.imgur.com/Kjrqx.png" rel="nofollow">enter image description here</a></p>

<p>I read others posts but no-one solved my problem, since they threats or only one key or they don't show how to access the different values inside the dictionary to compute the mean,median and variance.
The problem is simple, admitting that the dictionary implementation is ok, in which way must I access the different values for the key1:www.google.it -> key2:adblock ?
Any kind oh help is accepted and I'm available for any other answer.</p>

<p>You can do what you want using a dictionary, but you should really consider using the <a href="http://pandas.pydata.org/" rel="nofollow">Pandas</a> library. This library is centered around tabular data structure called "DataFrame" that excels in column-wise and row-wise calculations such as the one that you seem to need.</p>

<p>To get you started, here is the Pandas code that reads <em>one</em> text file using the <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_fwf.html#pandas.read_fwf" rel="nofollow">read_fwf()</a> method. It also displays the mean and variance for the fourth column:</p>

<pre class="lang-py prettyprint-override"><code># import the Pandas library:
import pandas as pd

# Read the file 'table.txt' into a DataFrame object. Assume
# a header-less, fixed-width file like in your example:
df = pd.read_fwf("table.txt", header=None)

# Show the content of the DataFrame object:
print(df)

# Print the fourth column (zero-indexed):
print(df[3])

# Print the mean for the fourth column:
print(df[3].mean())

# Print the variance for the fourth column:
print(df[3].var())
</code></pre>

<p>There are <a href="http://pandas.pydata.org/pandas-docs/stable/indexing.html" rel="nofollow">different ways of selecting columns and rows</a> from a DataFrame object. The square brackets <code>[ ]</code> in the previous examples selected a column in the data frame by column number. If you want to calculate the mean of the fourth column only from those rows that contain <code>adblock</code> in the third column, you can do it like so:</p>

<pre class="lang-py prettyprint-override"><code># Print those rows from the data frame that have the value 'adblock'
# in the third column (zero-indexed):
print(df[df[2] == "adblock"])

# Print only the fourth column (zero-indexed) from that data frame:
print(df[df[2] == "adblock"][3])

# Print the mean of the fourth column from that data frame:
print(df[df[2] == "adblock"][3].mean())
</code></pre>

<p><strong>EDIT:</strong>
You can also calculate the mean or variance for more than one column at the same time:</p>

<pre class="lang-py prettyprint-override"><code># Use a list of column numbers to calculate the mean for all of them
# at the same time:

l = [3, 4, 5]
print(df[l].mean())
</code></pre>

<p><strong>END EDIT</strong></p>

<p>If you want to read the data from several files and do the calculations for the concatenated data, you can use the <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.concat.html#pandas.concat" rel="nofollow">concat()</a> method. This method takes a list of DataFrame objects and concatenates them (by default, row-wise). Use the following line to create a DataFrame from all <code>*.txt</code> files in your directory:</p>

<pre class="lang-py prettyprint-override"><code>df = pd.concat([pd.read_fwf(file, header=None) for file in glob.glob("*.txt")],
               ignore_index=True)
</code></pre>

python dictionary