Doubt Python 3 Selecting items from a dataframe

Asked

Viewed 21 times

1

Good evening everyone, all right? I’m new to Python and I was doing some exercises where I needed to select some information from a database. In one of the subitens, the correct code was:

halftime_musicians[halftime_musicians.super_bowl <= 27]

The next, the correct code was:

# Display musicians with more than one halftime show appearance
halftime_appearances[haltime_appearances['super_bowl'] <=2** ]

Because in the first case, to reference a Super Mouse column I used the coluna.super_bowl and in the second case I had to use coluna['super_bowl'] instead of coluna.super_bowl?

1 answer

0

It is the 'same thing'. You have the two possibilities to access the same things. The author of the exercise wanted to show you that you can do one way or another that the result will be the same.

That:

halftime_musicians[halftime_musicians.super_bowl <= 27]

Amounts to this:

halftime_musicians[halftime_musicians['super_bowl'] <= 27]

This is a dataframe attribute: halftime_musicians.super_bowl, it will only 'exist' after the super_bowl column has been created.

Already with the notation df['coluna'] = 1 you can create and assign a value without it existing. And then it will be available in df.coluna or df['coluna'].

  • 1

    Just adding something useful to the topic, another option, and even more recommended is to use the query function, this way (for example): halftime_musicians.query('super_bowl <= 27')

Browser other questions tagged

You are not signed in. Login or sign up in order to post.