I have a pandas DataFrame, st
containing multiple columns:
DatetimeIndex: 53732 entries, 1993-01-0
This was solved here: Apply pandas function to column to create multiple new columns?
Applied to your question this should work:
def calculate(s):
a = s['path'] + 2*s['row'] # Simple calc for example
b = s['path'] * 0.153
return pd.Series({'col1': a, 'col2': b})
df = df.merge(df.apply(calculate, axis=1), left_index=True, right_index=True)
Yet another solution based on Assigning New Columns in Method Chains:
st.assign(a = st['path'] + 2*st['row'], b = st['path'] * 0.153)
Be aware assign
always returns a copy of the data, leaving the original DataFrame untouched.
To make the first approach work, try returning a Series instead of a tuple (apply is throwing an exception because it doesn't know how to glue the rows back together as the number of columns doesn't match the original frame).
def calculate(s):
a = s['path'] + 2*s['row'] # Simple calc for example
b = s['path'] * 0.153
return pd.Series(dict(col1=a, col2=b))
The second approach should work if you replace:
st.ix[i]['a'] = a
with:
st.ix[i, 'a'] = a
I always use lambdas and the built-in map()
function to create new rows by combining other rows:
st['a'] = map(lambda path, row: path + 2 * row, st['path'], st['row'])
It might be slightly more complicated than necessary for doing linear combinations of numerical columns. On the other hand, I feel it's good to adopt as a convention as it can be used with more complicated combinations of rows (e.g. working with strings) or filling missing data in a column using functions of the other columns.
For example, lets say you have a table with columns gender, and title, and some of the titles are missing. You can fill them with a function as follows:
title_dict = {'male': 'mr.', 'female': 'ms.'}
table['title'] = map(lambda title,
gender: title if title != None else title_dict[gender],
table['title'], table['gender'])