Skip to content

Instantly share code, notes, and snippets.

@ashic
Created December 14, 2022 11:13
Show Gist options
  • Save ashic/28926938d6707c0ee203f374a3384212 to your computer and use it in GitHub Desktop.
Save ashic/28926938d6707c0ee203f374a3384212 to your computer and use it in GitHub Desktop.
Python being Javascript
Python 3.9.7 (default, Sep 16 2021, 13:09:58)
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas as pd
>>> df_1 = pd.DataFrame(columns=['a', 'b'])
>>> df_2 = pd.DataFrame([{'a':42, 'b':43}, {'a':47, 'b':48}])
>>> def foo(row, to_add):
... print('hi')
... row['a'] = row['a'] + to_add
... print('bye')
... return row
...
>>> df_2.apply(lambda row: foo(row, 2), axis=1)
hi
bye
hi
bye
a b
0 44 43
1 49 48
>>> df_1.apply(lambda row: foo(row, 2), axis=1)
hi
Empty DataFrame
Columns: [a, b]
Index: []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment