Search code examples
pythonstringdataframecontainstokenize

check if the contents of data frame attribute has a string value 'OCC' anywhere


I am trying to check if a data frame attribute value contains a particular string or not. This is the code snippet:

#messages extracted from pst into data frame
message_df = pd.DataFrame(message_data, columns = ['Subject', 'Body', 'Read Flag'])

#want to check if subject in each of the mail contains OCC word or not
for index, row in message_df.iterrows():
    #print("\n For mail # ", index, "Subject is: ", row['Subject'],"\n Body is: ",  row['Body'],"\n Read Flag is: ",  row['Read Flag'], "\n")
    
    if row[row['Subject'].str.contains("OCC")]:
        print("Subject contains OCC: ", row['Subject'])

getting error :

'str' object has no attribute 'str'


I could print the message successfully with row['Subject'] but not able to tokenize it for comparison


Solution

  • row returns a series. Also row['Subject'] returns a string value. That means you cannot use str.contains. You should use in:

        if "OCC" in row['Subject']:
            print("Subject contains OCC: ", row['Subject'])