简体   繁体   中英

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

i have data csv

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

and I want to check whether the word already exists in CSV data

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM