条件にpassがある関数をapplyするとNaNが返ってくる[Python][Pandas]

7月 25, 2020

import pandas as pd

def func(row):
    if row['A'] < 3:
        return row['A'] + row['B']
    else:
        pass

df = pd.DataFrame({'A':[1,2,3],'B':[1,4,9],'C':[1,8,27]})
df['D'] = df.apply(func, axis=1)

>>> df
   A  B   C    D
0  1  1   1  2.0
1  2  4   8  6.0
2  3  9  27  NaN

Pandas,Python

Posted by vastee