簡體   English   中英

如何使用Python檢查數據是否在CSV文件中

[英]How to check whether the data is in a CSV file using Python

我有數據csv

word,label
relax,1
angry,0
happy,1
sad,3

我想檢查該詞是否已經存在於CSV數據中

我的代碼就是這樣,與我想要的不匹配

import pandas as pd
data = pd.read_csv('data/hasil_cluster2.csv',encoding = "ISO-8859-1") 
ip="key"
for index,row in data.iterrows():
    if ip is row[0]:
        print('data found')
        break
    else:
        print(row[0])

它檢查完整的數據幀,如果任何列中存在abc,則返回鍵,否則將返回這些行,否則為空

key = 'abc'
df = df[df.eq(key).any(axis=1)]
if df.empty:
    print("empty")
else:
   print("key found")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM