python - Converting Columns and Performing a function on them within Pandas -
i importing text file , adding 2 columns , attempting perform basic math on 2 newly created columns based on 2 other existing columns. periodically data structure of original text file changes in column length 10 columns 7. trying catch if else statement. error below. should converting to? , how can perform function on column identifying column number rather header name instead of mru['t1'] = math.sqrt(mru['r1']**2 + mru['p1']**2) mru['t1'] = math.sqrt(mru[1]**2 + mru[2]**2)?
"cannot convert series {0}".format(str(converter))) typeerror: cannot convert series <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 snippet of 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
you can't use normal math functions on series arrays use np.sqrt:
import numpy np mru['t1'] = np.sqrt(mru['r1']**2 + mru['p1']**2) the typeerror telling expecting float , not pandas series:
typeerror: cannot convert series <type 'float'> as other problem after you've named cols can filter them using list comprehension:
p_cols = [col col in df if 'p' in col] then generate same t , r cols , iterate on each of them in tandem , select 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 col in df if 'h' in col] p_cols = [col col in df if 'p' in col] 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) # thi #df[t_col] = np.sqrt(r**2 + p**2) h1 p1 t1 h2 p2 t2 h3 p3 t3 so above shows skeleton of how modify code achieve want in dynamic way
Comments
Post a Comment