user9988494
user9988494

Reputation:

The truth value of a DataFrame is ambiguous

I am trying to get the values that are related to brand and manufacturer which are same (e.g brand==J.R. Watkins and manufacturer==J.R.Watkins)in last elif block.But it giving error as:

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). My code is:

import csv
import pandas as pd
import sys
class sample:
        def create_df(self, f):
                self.z=pd.read_csv(f)

        def get_resultant_df(self, list_cols):
                self.data_frame = self.z[list_cols[:]]

        def process_df(self, df, conditions):
                resultant_df = self.data_frame

                if conditions[2] == 'equals':
                        new_df=resultant_df[resultant_df[conditions[1]] == conditions[3]]
                        return new_df
                elif conditions[2] == 'contains':
                        new_df = resultant_df[resultant_df[conditions[1]].str.contains(conditions[3])]
                        return new_df
                elif conditions[2] == 'not equals':
                        new_df = resultant_df[resultant_df[conditions[1]] != conditions[3]]
                        return new_df
                elif conditions[2] == 'startswith':
                        new_df = resultant_df[resultant_df[conditions[1]].str.startswith(conditions[3])]
                        return new_df
                elif conditions[2] == 'in':
                        new_df = resultant_df[resultant_df[conditions[1]].isin(resultant_df[conditions[3]])]
                        return new_df
                elif conditions[2] == 'not in':
                        new_df = resultant_df[~resultant_df[conditions[1]].isin(resultant_df[conditions[3]])]
                        return new_df
                elif conditions[2]=='group':
                        new_df=list(resultant_df.groupby(conditions[0])[conditions[1]])
                        return new_df
                elif conditions[2]=='specific':
                        new_df=resultant_df.loc[resultant_df[conditions[0]]==conditions[8]]
                        return new_df
                elif conditions[2]=='same':
                        if(resultant_df.loc[(resultant_df[conditions[0]]==conditions[8]) & (resultant_df[conditions[1]]==conditions[8])]).all():
                                new_df=resultant_df
                                return new_df
if __name__ == '__main__':
        sample = sample()
        sample.create_df("/home/purpletalk/GrammarandProductReviews.csv")
        df = sample.get_resultant_df(['brand', 'reviews.id','manufacturer','reviews.title','reviews.username'])
        new_df = sample.process_df(df, ['brand','manufacturer','same','manufacturer', 'size', 'equal',8,700,'J.R. Watkins'])
        print new_df['brand']

Upvotes: 0

Views: 3362

Answers (1)

jpp
jpp

Reputation: 164773

I am trying to get the values that are related to brand and manufacturer which are same (e.g brand==J.R. Watkins and manufacturer==J.R.Watkins)

Your logic is overcomplicated. Just apply a filter:

df = df[(df['brand'] == 'J.R. Watkins') & (df['manufacturer'] == 'J.R.Watkins')]

You don't need pd.DataFrame.all(), which appears to be what you are attempting. Nor do you need an inner if statement: if there's no match, you will have an empty dataframe.

Upvotes: 2

Related Questions