错误:如果满足两个条件,则将字符串拆分为两列时,DataFrame 的真值不明确

Error: The truth value of a DataFrame is ambiguous when splitting strings into two columns if two conditions are met

提问人:Lap Pan Chong 提问时间:5/31/2020 最后编辑:Lap Pan Chong 更新时间:6/2/2020 访问量:259

问:

如果满足以下两个条件,我正在尝试将字符串拆分为 column['first']。

  1. column['first'] 包含单词 'floor' 或 'floors'
  2. column['second'] 为空

但是,我收到了一条错误消息。

DataFrame 的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。

下面是我的代码

#boolean series for condition 1: when values in column['second'] are empty

only_first_token = pd.isna(results_threshold_50_split_ownership['second']) 
print (len(only_first_token)) 
print (type(only_first_token))

#boolean series for condition 2: when values in column['first'] contain string floor or floors

first_token_contain_floor = results_threshold_50_split_ownership['first'].str.contains('floors|floor',case=False)
print (len(first_token_contain_floor))
print (type(only_first_token))

#if both conditions are met, the string in column['first'] will be split into column['first'] and['second']

if results_threshold_50_split_ownership[(only_first_token) & (first_token_contain_floor)]:
    results_threshold_50_split_ownership.first.str.split('Floors|Floor', expand=True)

print(results_threshold_50_split_ownership['first'])

我在这里阅读了一些答案,并且已经更改了几次代码。我确保布尔值的总数在 1016 处相同。如果我删除 .所以我不明白为什么它是模棱两可的。if

任何帮助将不胜感激。非常感谢。

python pandas if-statement 布尔逻辑

评论

0赞 tdelaney 6/2/2020
请发布回溯,以便我们一起看到行和错误。

答:

1赞 Stef 6/1/2020 #1

你的条件完全没问题,问题是 if 语句 - 它的内容是:

if boolean_array :
  ...

但只需要一个布尔值,而不是整个布尔值数组。为了将布尔数组减少到只有一个值,您可以使用例如 any() 或 all(),如错误消息所示 - 等等。ifif all(boolean_array):

你真正想做的可能是:

results_threshold_50_split_ownership[(only_first_token) & (first_token_contain_floor)]['first'].str.split('Floors|Floor', expand=True)

即使用布尔数组进行布尔索引。

根据下面的注释进行更新:
您可以使用 将拆分结果分配给原始列。但是,在这种情况下,您需要通过在 split 函数中指定来确保最多返回 2 列(以防您的第一列多次包含单词“floor”)。
例:
results_threshold_50_split_ownership.loc[(only_first_token) & (first_token_contain_floor), ['first', 'second']]n=1

results_threshold_50_split_ownership = pd.DataFrame({'first': ['first floor value', 'all floors values', 'x'],
                                                     'second': ['y', None, None]})
print(results_threshold_50_split_ownership)
#               first second
#0  first floor value      y
#1  all floors values   None
#2                  x   None
only_first_token = pd.isna(results_threshold_50_split_ownership['second'])
first_token_contain_floor = results_threshold_50_split_ownership['first'].str.contains('floors|floor',case=False)
results_threshold_50_split_ownership.loc[(only_first_token) & (first_token_contain_floor), ['first', 'second']] = results_threshold_50_split_ownership[(only_first_token) & (first_token_contain_floor)]['first'].str.split('floors|floor', 1, expand=True).to_numpy()
print(results_threshold_50_split_ownership)
#               first   second
#0  first floor value        y
#1               all    values
#2                  x     None

评论

0赞 Lap Pan Chong 6/1/2020
谢谢你,@Stef!您的代码会有所帮助。然后,我使用函数将相关字符串拆分为现有数据框的两个单独的新列。我想知道是否有任何方法可以替换数据框现有列中那些与条件匹配的行的现有值,即“first”和?joinsecond
0赞 Stef 6/2/2020
请参阅我更新的答案(希望我理解正确,如果没有,请提供所需的输出)。
0赞 Lap Pan Chong 6/3/2020
你是明星!谢谢你,史蒂夫!