def tFunc(row):
if (random.random()>0.5):
info={'A': 'a', 'B': 'b', 'C': 'c', 'D': 'd'}
else:
info={'A': 'a', 'B': 'b', 'C': 'c'}
return info
# Workaround
# for key in info:
# row[key]=info[key]
# return row
df.apply(tFunc, axis=1, result_type='expand')
Consider an existing df. tFunc is a function that returns a dictionary info. Want to expand the existing df. Is there any better/faster way than the workaround method?
You can
jointhe output of yourapplycall:example with an input DataFrame that has one column "col":
Another (not recommended) option might be to
combine_firstthe dictionary as Series to the input row and return this. The drawback is that it might affect the original dtypes:example output: