What is the best option to create new DataFrame from a function applied to each row of a data frame. The ultimate goal is to concat (rbind) all the resulting new_dataframes.
Input:
Name Age
0 tom 10
1 nick 15
2 juli 14
Example:
import pandas as pd
import pdb
data = [['tom', 10], ['nick', 15], ['juli', 14]]
df = pd.DataFrame(data, columns=['Name', 'Age'])
def foo(row):
#pdb.set_trace()
new_df = row.to_frame(name='Values')
new_df.loc[new_df.index=='Name','New_column'] = 'Surname'
new_df.loc[new_df.index=='Age','New_column'] = '+5 months'
return new_df
df.apply(foo, axis=1)
Output:
data = {'Values':['Tom', '10', 'nich', '15', 'juli', '14'],
'New_column': ['Surname', '+5 months', 'Surname', '+5 months', 'Surname',
'+5 months']}
output = pd.DataFrame(data)
Values New_column
0 Tom Surname
1 10 +5 months
2 nich Surname
3 15 +5 months
4 juli Surname
5 14 +5 months
If .apply() is not the best option, I would appreciate an alternative.
For R users, I am looking for do.call(rbind, sapply())
Thanks.