1

I can't select and filter multiple columns together in Pandas. 'Name' and 'Year of Rank' are column names. Thank you!

dw[dw.Name=='El Toro' & dw['Name', 'Year of Rank']]
Alper
  • 51
  • 2
  • 5
  • This is literally the first thing you learn when reading about [pandas selecting](https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html) . Try the docs in the beginning, it's likely that will be very helpful – rafaelc Feb 14 '20 at 16:35

3 Answers3

4

I believe this is what you want:

dw[dw['Name'] == 'El Toro'][['Name','Year of Rank']]

or alternatively:

dw.loc[ dw['Name'] == 'El Toro', ['Name','Year of Rank']]

Edit: As pointed out in the comments, the second one is much preferred as it deals with the filtering and selection as a single entity.

Adam B.
  • 192
  • 10
0
import pandas as pd

dw = pd.DataFrame([
  {'Name': 'A', 'Year of Rank':1992, 'Rank': 1},
  {'Name': 'El Toro', 'Year of Rank':1993, 'Rank': 2},
  {'Name': 'C', 'Year of Rank':1994, 'Rank': 3}])

dw[dw.Name == 'El Toro'][['Name', 'Year of Rank']]

Like this?

Next time you try provide more context like a dummy dataframe.

anky
  • 74,114
  • 11
  • 41
  • 70
debugme
  • 1,041
  • 1
  • 10
  • 22
0

The following code should work:

dw[dw['Name'] == 'El Toro'][['Name','Year of Rank']]
Xpie
  • 43
  • 6