Pandas & Seaborn – IndexingError: Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match)

  boolean, indexing, pandas, python, seaborn

The CSV files are from here: https://ali2.wanmeide.xyz/ameya225/TDI/tree/master/Allen_Brain_Atlas/AD

I ran this on Google Colab, expecting a violinplot, but I got "IndexingError: Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match)" instead. I’m not sure what this means nor what I need to do in order to resolve it. Does it have to do with the fact that the hue and the x & y are from different data frames, or does it have something to do with the data=… part. I’m unsure of how to resolve the issue.

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
from urllib.request import urlopen
from bs4 import BeautifulSoup
from google.colab import drive
drive.mount('/content/drive')

DonorInfo = pd.read_csv("drive/MyDrive/Data/DonorInformation.csv")

ProteinAndPathologyQuantifications = pd.read_csv("drive/MyDrive/Data/ProteinAndPathologyQuantifications.csv")

apo_e4_present = DonorInfo.loc[DonorInfo["apo_e4_allele"] == "Y"]

apo_e4_present_alzheimer_positive = apo_e4_present.loc[apo_e4_present["dsm_iv_clinical_diagnosis"] == "Alzheimer's Disease Type"]

Alzheimers_patients_DI = DonorInfo[DonorInfo["dsm_iv_clinical_diagnosis"] == "Alzheimer's Disease Type"]

Alzheimers_patients_PAPQ = ProteinAndPathologyQuantifications.loc[(ProteinAndPathologyQuantifications["donor_id"].isin(Alzheimers_patients_DI.donor_id))]

ax = sns.violinplot(x=Alzheimers_patients_PAPQ.structure_acronym, 
                    y=Alzheimers_patients_PAPQ.bdnf_pg_per_mg, 
                    hue=Alzheimers_patients_DI.apo_e4_allele, 
                    data=Alzheimers_patients_PAPQ,
                    palette="Set2",  
                    split=True,
                    order=('FWM', 'TCx', 'PCx', 'HIP'))

I was expecting the plot to resemble the image below.

(image source: https://seaborn.pydata.org/generated/seaborn.violinplot.html)

Picture of the error I got

Source: Python Questions

LEAVE A COMMENT