Pandas,用条件组合字符串列,但是得到一个Series的真值不明确

Pandas, Combine string columns with conditions, but get The truth value of a Series is ambiguous

我想加入有条件的 Pandas DataFrame 列——仅当结尾不为空时才用逗号分隔列:

import numpy as np
import pandas as pd

df = pd.DataFrame({'score':np.random.randn(3),
                   'person1':[x*3 for x in list('ABC')],
                  'person2':[x*3 for x in list('DEF')]})
df

df['person2'][1]=""
#print(df['person1']+("" if df['person2']=="" else ", "+df['person2']) )
#print(df['person1']+("" if not df['person2'] else ", "+df['person2']) )
#print(df['person1']+("" if not df['person2'].values else ", "+df['person2']) )
# ValueError: The truth value of a Series is ambiguous.

但是,我从搜索结果中得到的所有建议都不起作用。我上面所有的尝试都以错误结束:

ValueError: The truth value of a Series is ambiguous.

PS,考虑到我是join多个列,有些列可能是空白的,不想以多个逗号组成的字符串结尾,可不可以写个函数这样我就可以在连接多个列时调用该函数?

请帮忙。谢谢

您可以像这样对每一行应用一个函数:

df.apply(lambda row: row.person1 + (',' + row.person2 if row.person2 else ''),
         axis=1)