Q:

How to generalize this calculation with a pandas DataFrame to any number of columns?

I have a file with some data that looks like
1 2 3 4
2 3 4 5
3 4 5 6
4 5 6 7

I can process this data and do math on it just fine:
import sys
import numpy as np
import pandas as pd

def main():
    if(len(sys.argv) != 2):
        print "Takes one filename as argument"
        sys.exit()

    file_name = sys.argv[1]

    data = pd.read_csv(file_name, sep=" ", header=None)
    data.columns = ["timestep", "mux", "muy", "muz"]

    t = data["timestep"].count()

    c = np.zeros(t)

    for i in range(0,t):
        for j in range(0,i+1):
            c[i-j] += data["mux"][i-j] * data["mux"][i]
            c[i-j] += data["muy"][i-j] * data["muy"][i] 
            c[i-j] += data["muz"][i-j] * data["muz"][i]

    for i in range(t):
        print c[i]/(t-i)

The expected result for my sample input above is
42.5
62.0
84.5
110.0

This math is finding the time correlation function for my data, which is the time-average of all permutations of the pairs of products in each column.
I would like to generalize this program to 

work on n number of columns (in the i/j loop for example), and
be able to read in the column names from the file, so as to not have them hard-coded in

Which numpy or pandas methods can I use to accomplish this?

A:

We can reduce it to one loop, as we would make use of array-slicing and use sum ufunc to operate along the rows of the dataframe and thus in the process make it generic to cover any number of columns, like so -
a = data.values
t = data["timestep"].count()
c = np.zeros(t)
for i in range(t):
    c[:i+1] += (a[:i+1,1:]*a[i,1:]).sum(axis=1)

Explanation
1) a[:i+1,1:] is the slice of all rows until the i+1-th row and all columns starting from the second column, i.e mux, muy and so on.
2) Similarly, for [i,1:], that's the i-th row and all columns from second column onwards.
To keep it "pandas-way", simply replace a[ with data.iloc[.

