Q:

Converting Columns and Performing a function on them within Pandas

I am importing a text file and adding two columns and attempting to perform some basic math on the two newly created columns based on two other  existing columns.  Periodically the data structure of my original text file changes in column length from 10 columns to 7.  So I am trying to catch this with an If else statement. But I get the error below.  What should I be converting this to?  and how can I perform the function on the column by identifying it by a column number rather than header name so instead of mru['t1'] = math.sqrt(mru['r1']**2 + mru['p1']**2) something like this mru['t1'] = math.sqrt(mru[1]**2 + mru[2]**2)?
"cannot convert the series to {0}".format(str(converter)))
TypeError: cannot convert the series to <type 'float'>

My Code is:
mru = pd.read_csv(r"C:\some.txt", skipinitialspace=True, names=['time', 'r1', 'p1', 'h1', 'r2', 'p2', 'h2', 'r3', 'p3', 'h3'])

#Identify colum number
col = len(mru.columns)

#Caluulate Tilt
if col == 10:
    converted = mru[mru.columns[-9:]].convert_objects(convert_numeric=True)
    mru[mru.columns[-9:]] = converted
    mru['t1'] = math.sqrt(mru['r1']**2 + mru['p1']**2)
    mru['t2'] = math.sqrt(mru['r2']**2 + mru['p2']**2)
    mru['t3'] = math.sqrt(mru['r3']**2 + mru['p3']**2)
else:
    converted = mru[mru.columns[-9:]].convert_objects(convert_numeric=True)
    mru[mru.columns[-6:]] = converted
    mru = pd.read_csv(r"C:\Dan\20150330_150831_C.txt", skipinitialspace=True, names=['time', 'r1', 'p1', 'h1', 'r2', 'p2', 'h2'])
    mru['t1']= math.sqrt(mru['r1']**2 + mru['p1']**2)
    mru['t2'] = math.sqrt(mru['r2']**2 + mru['p2']**2)

and a snippet of my data is: (10 column example):
15:08:31.898,-0.3000,0.1400,0.0000,-0.3100,0.5300,0.6234,0.3357,-0.1500,0.0000
15:08:32.898,-0.3000,0.1400,0.0000,-0.1500,0.2800,-0.0984,0.0905,0.0100,0.0000

A:

You can't use normal math functions on Series which are arrays use np.sqrt:
import numpy as np
mru['t1'] = np.sqrt(mru['r1']**2 + mru['p1']**2)

The TypeError is telling you that it is expecting a float and not a pandas Series:
TypeError: cannot convert the series to <type 'float'>

As for your other problem after you've named your cols you can filter them using a list comprehension:
p_cols = [col for col in df if 'p' in col]

then just generate the same for t and r cols and then iterate over each of them in tandem and select the cols:
In [76]:

df = pd.DataFrame(columns = ['time', 'r1', 'p1', 'h1', 'r2', 'p2', 'h2', 'r3', 'p3', 'h3'])
df
Out[76]:
Empty DataFrame
Columns: [time, r1, p1, h1, r2, p2, h2, r3, p3, h3]
Index: []
In [83]:

r_cols = [col for col in df if 'h' in col]
p_cols = [col for col in df if 'p' in col]
for i in range(3):
    r = df[r_cols[i]]
    p = df[p_cols[i]]
    t_col = 't'+str(i+1)
    print(r_cols[i], p_cols[i], t_col)
    # do something like thi
    #df[t_col] = np.sqrt(r**2 + p**2)

h1 p1 t1
h2 p2 t2
h3 p3 t3

So the above shows a skeleton of how you could modify your code to achieve what you want in a dynamic way

