你知道是否有可能在2列中返回2个值与一个def?
我有两个def:
def codeAcro (row):
NOVA, Concordance = row['Code'], row['Concordance']
if pd.isna(Concordance) == True:
return None
else:
return Concordance
ListeCodeNova['Acronyme'] = ListeCodeNova[['Concordance','Code']].apply(codeAcro, axis=1)
def codeNOVA (row):
NOVA, Concordance = row['Code'], row['Concordance']
if pd.isna(Concordance) == True:
return None
else:
return NOVA
ListeCodeNova['Concordance'] = ListeCodeNova[['Concordance','Code']].apply(codeNOVA, axis=1)
我想知道是否有可能在一个def中简化。
为了获得更好的性能,避免apply方法(对每一行调用),并使用布尔索引代替:
import pandas as pd
import numpy as np
df = pd.DataFrame({'Concordance':[1, np.nan, 2], 'Code':[10, 11,12]})
mask = df.Concordance.notna()
df['Acronyme'] = np.where(mask, df.Concordance, None)
df['Concordance'] = np.where(mask, df.Code, None)