I am trying to run apply in pandas.DataFrame so that a function would run through the whole table, taking a few column fields as input, and generate multiple new fields at the same time, and once the scan is done, the new fields could form extra multiple new columns.
Conceptually the following describes what I need: to apply a function f to the DataFrame column-wise to generate multiple new columns at the same time:
f :: field1, field2, field3, ... -> newfield1, newfield2,...
when I apply this function to the DataFrame, it gives me
f' :: column1, column2, column3, ... -> newcolumn1, newcolumn2, ...
Here is an example:
>>> df
denominator numerator
0 3 10
1 5 12
2 7 14
I would like to create two more columns, quotient and remainder.
In this particular example I could run // and % separately because it is trivial but it is not the preferred because I can technically get both quotient and remainder at the same time. In some real world cases, getting them at the same time is more efficient.
The following is what I came up with but I don't know if it is the most pythonic way of doing it. How df.apply turns a sequence of row-based pd.Series into columns is also not clear to me.
>>> def rundivmod(n, d):
... q, r = divmod(n, d)
... return {'quotient': q, 'remainder': r}
>>> pd.merge(df, df.apply(lambda row: pd.Series(rundivmod(row.numerator, row.denominator)), axis=1), left_index=True, right_index=True)
denominator numerator quotient remainder
0 3 10 3 1
1 5 12 2 2
2 7 14 2 0
EDIT: removed my other method to generate quotient and remainder separately as they are misleading in this case.
df['quotient'], df['remainder'] = df['numerator']//df['denominator'], df['numerator'] % df['denominator']?