[英]How to check whether the data is in a CSV file using Python
i have data csv 我有数据csv
word,label
relax,1
angry,0
happy,1
sad,3
and I want to check whether the word already exists in CSV data 我想检查该词是否已经存在于CSV数据中
My code is like this and doesn't match what I want 我的代码就是这样,与我想要的不匹配
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])
It check complete dataframe the key if abc exist in any column it would return those rows else it would be empty 它检查完整的数据帧,如果任何列中存在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.