더 많은 자유를 위해 numpy로 이동할 수 있습니다. else 문 하나만있는 경우 np.where
을 사용하십시오. 당신이 dataframe이있는 경우 pd.np
즉
를 사용하여 팬더에서 NumPy와 libarary에 액세스 할 수 있습니다
df = pd.DataFrame({'col1':[1,2,3,4,5],'col2':[1,3,4,5,2]})
df['where'] = pd.np.where(df['col1']>df['col2'], df['col1']+df['col2'], df['col1']+df['col2']+1)
col1 col2 where
0 1 1 3
1 2 3 6
2 3 4 8
3 4 5 10
4 5 2 7
# Not exactly by much like
#if df['col1']>df['col2']:
# return df['col1']+df['col2']
#else:
# return df['col1']+df['col2']+1
당신이 다른 하나 이상있는 경우 많은 다른 사다리는 다음 np.select
m1 = df['col1']<df['col2']
m2 = df['col1']>df['col2']
df['select'] = pd.np.select([m1,m2], [df['col1']+df['col2'],0], 'equal')
# all conditions, all executables, final else
col1 col2 where select
0 1 1 3 equal
1 2 3 6 5
2 3 4 8 7
3 4 5 10 9
4 5 2 7 0
#Which is much like
#if df['col1']< df['col2']:
# return df['col1'] + df['col2']
#elif df['col1']>df['col2']:
# return 0
#else
# return 'equal'
내가 갈 경우처럼 [부울 배열] (https://docs.scipy.org/doc/numpy-1.13.0/user/basics.indexing.html#boolean-or-mask-index-arrays)에 대해서도 읽어 보시기 바랍니다. 그들은 numpy와 pandas 둘 다로 작동합니다. – Georgy