2

I'm trying to plot a graph for only rows with an if condition. And to the best of my knowledge, this is what I've done.

b = pd.read_csv('Zlatan_ClubComp.csv')

print(b.head())

if (b['Competition_Type'] == 'League'):
    pp = sns.pairplot(b, y_vars=['Appearances'], x_vars=['Goals_PerGame', 'Minutes_PerGoal'], hue="Club")
    plt.show()

Zlatan_ClubComp is the csv file from which I get my data. I've run the code and this is the error I get,

Traceback (most recent call last):
  File "C:/Users/Siddhardh/Desktop/Python/Projects/Zlatan_Analysis/PairPlots.py", line 13, in <module>
    if (b['Competition_Type'] == "League"):
  File "C:\Users\Siddhardh\Desktop\OiDS Project\Zlatan_Analysis\lib\site-packages\pandas\core\generic.py", line 1555, in __nonzero__
    self.__class__.__name__
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

From the Googling I've done, I know that pandas take truth values of conditional statements as ambiguous. But, I can't seem to fix this.

Siddarth Krishna S
  • 111
  • 1
  • 2
  • 10
  • The premise is flawed because if conditions can only be specified on scalars. You are applying a condition on a column which returns a column of booleans. Do you see why this does not work? Also, the answers in the duplicate should answer your question as well. – cs95 Dec 07 '19 at 00:28
  • I faced a similar problem and for me this error resulted when the `df` did not contain the column specified for the `hue` argument. – Sameen Aug 25 '21 at 16:33

1 Answers1

4
b = pd.read_csv('Zlatan_ClubComp.csv')

print(b.head())

b = b[b['Competition_Type'] == 'League']

pp = sns.pairplot(b, y_vars=['Appearances'], x_vars=['Goals_PerGame', 'Minutes_PerGoal'], hue="Club")
    plt.show()
milos.ai
  • 3,882
  • 7
  • 31
  • 33