Kair0
Kair0

Reputation: 135

Python pandas truth value of a DataFrame is ambiguous

I have several csv file and i want to look over a column and search a string value. With the code below i obtain an error : The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). I don't understand why.

import pandas as pd
import io

path = "C:\\Users\\9310136N\\Downloads\\test"


for filename in os.listdir(path):
    print(filename)
    data = pd.read_csv(path+"\\"+filename,  encoding='latin1', sep='|')
    if data[data['Code_ligne_ecriture'].apply(str).str.contains('0135250635')] :
        print ("L'écriture est dans le fichier " + filename)

Upvotes: 0

Views: 435

Answers (1)

Jose Manuel de Frutos
Jose Manuel de Frutos

Reputation: 994

data[data['Code_ligne_ecriture'].apply(str).str.contains('0135250635')]

It will return a boolean dataframe. However, the if conditional only accepts an expression that returns a Boolean.

It seems that you search if there is an occurrence of a certain string, you could use then a.any().

data[data['Code_ligne_ecriture'].apply(str).str.contains('0135250635')].any()

Upvotes: 1

Related Questions