Passing a function with multiple arguments to DataFrame.apply

It’s just the way you think it would be, apply accepts args and kwargs and passes them directly to some_func. df.apply(some_func, var1=’DOG’, axis=1) Or, df.apply(some_func, args=(‘DOG’, ), axis=1) 0 foo-x-DOG 1 bar-y-DOG dtype: object If for any reason that won’t work for your use case, then you can always fallback to using a lambda: df.apply(lambda … Read more

Using the same option multiple times in Python’s argparse

Here’s a parser that handles a repeated 2 argument optional – with names defined in the metavar: parser=argparse.ArgumentParser() parser.add_argument(‘-i’,’–input’,action=’append’,nargs=2, metavar=(‘url’,’name’),help=’help:’) In [295]: parser.print_help() usage: ipython2.7 [-h] [-i url name] optional arguments: -h, –help show this help message and exit -i url name, –input url name help: In [296]: parser.parse_args(‘-i one two -i three four’.split()) Out[296]: … Read more

tech