How to use convert with custom function and reference row
carlcorder opened this issue ยท 8 comments
Would it be possible to get an example placed in the documentation of using convert with a custom function that has reference to the row? I assume something like this should work?
table = [[a,b,c], [1,2,3]]
def my_func(row):
return row['b'] + row['c']
etl.convert(table, 'a', my_func)
# [[a,b,c], [5,2,3]]
Hi @carlcorder ,
Looks good to me.
Would you consider making a PR adding an example in the convert function documentation ?
I would be happy to review and integrate your proposal.
Sure.
Please, tell me if you would have any questions.
Thanks @javidy! I forgot that I also need to pass the pass_row=True
keyword in order for the above to work.
Thanks @carlcorder for comment. I'll take that into account.
For some reason I couldn't get it worked. When I try below code, I get None
instead of sum of fields.
Could you please check if I miss anything?
import petl as etl
def my_func(row):
return row.bar + row.baz
#row['bar'] + row['baz']
table1 = [['foo', 'bar', 'baz'], ['A', 2.4, 12]]
table2 = etl.convert(table1, 'foo', my_func, pass_row=True)
Hey Javidy, my hunch is that my_func
is not getting called. I think the issue here is that the signature of the function needs to be def my_func(val, row):
?
hey @carlcorder ,
you're right about that. It works when I change the signature of the function.
thanks for help :)