4

I've tried to create a graph for the dates column in python with seaborn. I'm getting the error bellow. Do you have any idea how to fix it?

----> 4 if df['MS_Date']=="s.xii" or df['MS_Date']=="s.xii(1)" or df['MS_Date']=="s.xii(2)" or df['MS_Date']=="s.xii(in)" or df['MS_Date']=="s.xii(ex)" or df['MS_Date']=="s.xii(med)": 5 df['MS_Date']== "12th century" 6 if df['MS_Date']=="s.xii/xiii" or df['MS_Date']=="s.xii/xiii":

~\anaconda\lib\site-packages\pandas\core\generic.py in nonzero(self) 1477 1478 By ambiguous, we mean that it matches both a level of the input -> 1479 axis and a label of the other axis. 1480 1481 Parameters

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

The code:

if df['MS_Date']=="s.xii" or df['MS_Date']=="s.xii(1)" or df['MS_Date']=="s.xii(2)" or df['MS_Date']=="s.xii(in)" or df['MS_Date']=="s.xii(ex)" or df['MS_Date']=="s.xii(med)": 
        df['MS_Date']== "12th century"
if df['MS_Date']=="s.xii/xiii" or df['MS_Date']=="s.xii/xiii":
    df['MS_Date']=="12 & 13 century"
if df['MS_Date']=="s.xiii" or df['MS_Date']=="s.xiii(1)" or df['MS_Date']=="s.xiii(2)" or df['MS_Date']=="s.xiii(in)" or df['MS_Date']=="s.xiii(ex)" or df['MS_Date']=="s.xiii(med)":
    df['MS_Date']=="13 century"
if df['MS_Date']=="s.xiii/s.xiv" or df['MS_Date']=="s.xiii/xiv":
    df['MS_Date']=="13 & 14 century"
if df['MS_Date']=="s.xiii/s.xv":
    df['MS_Date']=="13 & 15 century"
if  df['MS_Date']=="s.xiv" or df['MS_Date']=="s.xiv(1)" or df['MS_Date']=="s.xiv(2)" or df['MS_Date']=="s.xiv(in)" or df['MS_Date']=="s.xiv(ex)"  or df['MS_Date']=="s.xiv(med)": 
    df['MS_Date']=="14th century"
if  df['MS_Date']=="s.xiv/xv":
    df['MS_Date']== "14 & 15 century"
if df['MS_Date']=="s.xv" or df['MS_Date']=="s.xv(1)" or df['MS_Date']=="s.xv(2)" or df['MS_Date']=="s.xv(in)" or df['MS_Date']=="s.xv(ex)" or df['MS_Date']=="s.xv(med)" :
    df['MS_Date']=="15th century"
if  df['MS_Date']=="s.xv/xvi":
    df['MS_Date']== "15 & 16 century"
if df['MS_Date']=="xvi" or df['MS_Date']=="s.xvi(in)":
    df['MS_Date']== "16 century"
else:
    df['MS_Date']=="unknown"



import seaborn as sns
sns.set(style="whitegrid")
fig, ax = plt.subplots()
fig.set_size_inches(25, 12)
sns.countplot(x='MS_Date', data=df, order=df['MS_Date'].value_counts().index, palette="Greens_d")
sns.set(style="white")
dfnew=nbl.describe(include=['object'])
num=dfnew.loc['count','MS_Date']
# df.iloc[0]['MS_Date']
ax.set_title('Distrubution of all manuscripts '+str(num)+' manuscripts')
d12
  • 135
  • 3
  • 11
  • 1
    Better use `np.select` method. Will be easier for you to keep the track of conditions and value to be assigned. – Pygirl Aug 24 '20 at 12:59

1 Answers1

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

This error means you are getting a series of boolean values in place of a single boolean value. For using if and alse you need a single True/False value but here by using df['MS_Date']=="s.xii" you are getting a series which will give you all the locations with True/False based on criteria they meet.

sample df:

    Player      Position    color
0   Pele        Forward     black
1   Platini     Midfielder  white
2   Beckenbauer Defender    red

If I want to change the value of color column based on some criteria then instead of using if and else I will write this as like this:

df.loc[(df['Player']=='Pele') | (df['Position']=='Forward'), 'color'] = 'white'

Or

You can write it like this using numpy.where:

np.where(<condition>, <value1 if true>, <value2 if false>) 

df['color'] = np.where((df['Player']=='Pele') | (df['Position']=='Forward'), 'black', df.color)

To make it clean and concise use np.select see here how to use: https://stackoverflow.com/a/60244752/6660373

and also instead of using == you can use eq

I will use loc to get the location where my condition holds true. and on that position I will choose a column to change. df.loc[<conditions>, <column to assign>] = <value>

Use like this:

df.loc[(df['MS_Date']=="s.xii") | (df['MS_Date']=="s.xii(1)") | (df['MS_Date']=="s.xii(2)") | (df['MS_Date']=="s.xii(in)") | (df['MS_Date']=="s.xii(ex)") | (df['MS_Date']=="s.xii(med)"), 'MS_Date']= "12th century"
df.loc[(df['MS_Date']=="s.xii/xiii") | (df['MS_Date']=="s.xii/xiii"), 'MS_Date']="12 & 13 century"
Pygirl
  • 12,969
  • 5
  • 30
  • 43
  • Yes! it really helps! I just trying to figure out how to sort – d12 Aug 24 '20 at 15:16
  • @ Pygirl do you know how to sort? 12th, 13th, 14th and etc? sns.countplot(x='MS_Date', data=df, order=df['MS_Date'].value_counts().index, palette="Greens_d") – d12 Aug 24 '20 at 16:08
  • You have to create a custom.sort function. – Pygirl Aug 24 '20 at 17:17