“ValueError:在数据帧中使用带有lambdas的条件时,系列的真值是不明确的”

时间:2017-09-14 11:39:50

标签: python pandas lambda

我有dtlist 以及nn_language列名:dt = pd.DataFrame({"language1": ["english", "english123", "ingles", "ingles123", "14.0", "13", "french"], "language2": ["englesh", "english123", "ingles", "ingles123", "14", "13", "french"]}) nn_language = dt.columns[dt.columns.str.contains("language")]

编辑:添加了样本数据

dt[nn_language]

object的所有元素均为dt[nn_language]类型。 我想做的是,如果初始值为"english" like,则将("english","ingles",14)的初始值更改为"other",否则我想将初始值更改为{ {1}}

我尝试过:dt[nn_language].apply(lambda x: 'english' if x.str.contains('^engl|^ingl|14.0') else 'other')

但我收到错误ValueError: ('The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().',

Thisthis没有帮助我

2 个答案:

答案 0 :(得分:3)

使用isin

check = ["english","ingles", '14']
dt[nn_language].apply(lambda x: np.where(x.isin(check) , 'english', 'other'))

或者:

dt[nn_language].apply(lambda x: pd.Series(np.where(x.isin(check) , 'english', 'other')))

似乎你需要:

dt[nn_language].apply(lambda x: np.where(x.str.contains('^engl|^ingl|14.0')  , 'english', 'other'))

答案 1 :(得分:1)

您可以使用 df.apply np.where pd.Series.isin

dt[nn_language] = dt[nn_language].apply(lambda x:\
       np.where(x.isin(["english","ingles","14"]), 'english', 'other'))
df 
      Col1
0  english
1     test
2       14
3      foo
4   ingles

nn_language  = ['Col1']

df[nn_language] = df[nn_language].apply(lambda x: \
       np.where(x.isin(["english","ingles",'14']), 'english', 'other'))
df

     Col1
0  english
1    other
2  english
3    other
4  english