Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python - "Boolean Series key will be reindexed to match DataFrame index. from ipykernel import kernelapp as app"

menu_sub= menu[menu["Trans Fat"]==0][menu["Cholesterol (% Daily Value)"]==0][menu["Cholesterol (% Daily Value)"]==0]

returns: "Boolean Series key will be reindexed to match DataFrame index. from ipykernel import kernelapp as app"

After searching for a solution people suggested I used '&', but after using that I get a other error..

menu_sub= menu[menu["Trans Fat"]==0 & menu["Cholesterol (% Daily Value)"]==0 & menu["Cholesterol (% Daily Value)"]==0]     

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

Is there a way to do this in one line? or do have to create 3 different lines?

like image 217
Mark Wekking Avatar asked Mar 04 '18 14:03

Mark Wekking


1 Answers

First you were chain boolean slicing where the boolean series you were slicing with were based off of the original and the slices you were chaining kept getting smaller.

Second, you need to wrap your boolean series in parentheses.

menu_sub= menu[
    (menu["Trans Fat"] == 0) &
    (menu["Cholesterol (% Daily Value)"] == 0) &
    (menu["Cholesterol (% Daily Value)"] == 0)
]     

But now you can see you are repeating one condition twice, which I don't understand.

like image 163
piRSquared Avatar answered Nov 15 '22 07:11

piRSquared