我想要做的是让用户输入一组用空格分隔的值,读取它们,与数据框正确比较并相应地打印文本。经过数小时的努力,看似简单的任务却得到了解决。奇怪的是:
我将输入的 str 转换为 list,可以在代码示例中看到。所以,这显然与数据类型无关。任何帮助将不胜感激!
import pandas as pd in1 = input("Choose certain numbers separated with spaces:") in1 = in1.strip() result = pd.DataFrame([1, 1, 1, 2, 3, 3, 4, 5], columns=['Numbers']) uniq_numbers = list(result.Numbers.unique()) print(type(in1)) in1 = in1.split() for i in range(len(in1)): in1[i] = int(in1[i]) print(type(in1)) if all(result[(result['Numbers'].isin([in1]))]): print('All numbers', in1, ' are in dataframe:', uniq_numbers) elif not all(result['Numbers'].isin(in1)): print('No numbers', in1, ' were found in dataframe!', uniq_numbers) else: print('Some of the numbers entered:', in1, 'are NOT in dataframe, be aware. \n', 'Valid number/s:', set(in1).intersection(result), '\n Number/s that were NOT found in dataframe:', (set(in1) - set(result)) )
输入:6 7 输出:所有数字都在数据框 [‘6’, ‘ ‘, ‘7’] 中
有两个主要问题。您不是在测试列表中的所有数字是否都在数据框中,而是在测试数据框中的所有数字是否都在列表中。该语句的问题if在于,作为函数的参数,all您没有使用bools 的可迭代对象,而是使用对象,在这种情况下是数据框。
if
all
bool
要测试列表中的所有数字是否都在数据框中,您可以使用numpy:
numpy
import numpy as np import pandas as pd result = pd.DataFrame([1, 1, 1, 2, 3, 3, 4, 5], columns=['Numbers']) numbers = [1, 3, 5] mask = np.isin(numbers, result['Numbers'].unique()) if np.all(mask): print('All numbers present') elif not np.any(mask): print('None numbers present') else: print('Some numbers present')
要知道列表中的哪些数字出现在数据框中,您可以使用变量mask。如果是,numbers[i]则数据框中存在数字。如果将数字转换为数组,您可以简单地将所有当前数字作为mask[i]``True``numpy
mask
numbers[i]
mask[i]``True``numpy
numbers_present = np.array(numbers)[mask]
这是可能的,因为您可以numpy使用 s 索引数组bool。
请尝试以下操作:我认为它会解决您的问题:
import pandas as pd in1 = input("Choose certain numbers separated with spaces:") in1 = in1.strip() result = pd.DataFrame([1, 1, 1, 2, 3, 3, 4, 5], columns=['Numbers']) uniq_numbers = list(result.Numbers.unique()) print(type(in1)) in1 = in1.split() for i in range(len(in1)): in1[i] = int(in1[i]) print(type(in1)) in1_set = set(in1) uniq_numbers_set = set(uniq_numbers) if in1_set.issubset(uniq_numbers_set): print('All numbers', in1, ' are in dataframe:', uniq_numbers) elif not in1_set.intersection(uniq_numbers_set): print('No numbers', in1, ' were found in dataframe!', uniq_numbers) else: print('Some of the numbers entered:', in1, 'are NOT in dataframe, be aware. \n', 'Valid number/s:', in1_set.intersection(uniq_numbers_set), '\n Number/s that were NOT found in dataframe:', in1_set - uniq_numbers_set )
在这段更正后的代码中,我们转换in1并uniq_numbers设置并使用该issubset方法检查是否所有数字都in1在 DataFrame 中。我们还使用该intersection方法检查in1DataFrame 中是否有任何数字。
in1
uniq_numbers
issubset
intersection