Loading [MathJax]/extensions/tex2jax.js

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

7月 25, 2020

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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
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
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
0

Pandas,Python

Posted by vastee