Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
.loc with Booleans
#1
Can someone explain the mechanism of this output?

df = pd.DataFrame([[1, 2], [4, 5], [7, 8]],
     index=['cobra', 'viper', 'sidewinder'],
     columns=['max_speed', 'shield'])


>>> df.loc[[False,False,True]]
            max_speed  shield
sidewinder          7       8
Reply
#2
pandas.DataFrame.loc
loc doc Wrote:.loc[] is primarily label based, but may also be used with a boolean array.
So if True(a boolan) is in the list it will get that row or rows.
import pandas as pd

df = pd.DataFrame([[1, 2], [4, 5], [7, 8]],
     index=['cobra', 'viper', 'sidewinder'],
     columns=['max_speed', 'shield'])

>>> df
            max_speed  shield
cobra               1       2
viper               4       5
sidewinder          7       8

>>> df.loc[[False, True, True]]
            max_speed  shield
viper               4       5
sidewinder          7       8

>>> # Same as
>>> df.loc[['viper', 'sidewinder']]
            max_speed  shield
viper               4       5
sidewinder          7       8
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I find a string in sequence (with Booleans)? go127a 3 2,250 Apr-23-2019, 01:58 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020