【问题标题】:Check if multiple values exists in a DataFrame using Python IN operator使用 Python IN 运算符检查 DataFrame 中是否存在多个值
【发布时间】:2021-12-06 20:20:18
【问题描述】:

我想检查数据框中是否存在三个值,我不确定如何正确放置这些值。

我的代码会给我一个错误的答案,插入一个未来的警告。元素比较失败;而是返回标量,但将来会执行元素比较

details = {
    'Name' : ['Tom', 'Lee', 'Sara',
              'Shivangi', 'Priya', 'Swapnil'],
    'University' : ['BHU', 'JNU', 'DU', 'BHU', 'Geu', 'Geu'],
}
 
df = pd.DataFrame(details, columns = ['Name',  'University'])

if ['Tom', 'Lee', 'Sara'] in df.values:
    print("\nYes, These values exist in Dataframe")
 
else:
    print("\nNo,value not exists ")

由于数据框包含“Tom”、“Lee”、“Sara”,答案应该是肯定的。

【问题讨论】:

  • sum((df['Name'] == i).any() for i in ['Tom', 'Lee', 'Sara']) == 3

标签: python python-3.x dataframe


【解决方案1】:

斯科特·波士顿的功劳

if sum((df['Name'] == i).any() for i in ['Tom', 'Lee', 'Sara']) == 3:
    print("\nYes, These values exist in Dataframe")
 
else:
    print("\nNo,value not exists ")

【讨论】:

    猜你喜欢
    • 2021-02-24
    • 1970-01-01
    • 2022-06-14
    • 2021-10-18
    • 2019-09-24
    • 2018-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多