1

So I am trying to conditional selection by storing a string for A,(eg. A = 'foo') so that I can see the data for only "foo" or "bar." However, if I want to actually see everything, is there a string I can use to select everything? I'm sorry that this sound very confusing, let me demonstrate what I mean.

this is my data frame:

#      A      B  C   D
# 0  foo    one  0   0
# 1  bar    one  1   2
# 2  foo    two  2   4
# 3  bar  three  3   6
# 4  foo    two  4   8
# 5  bar    two  5  10
# 6  foo    one  6  12
# 7  foo  three  7  14

I can do the following to select only 'foo' in A.

selection = 'foo'
print(df.loc[df['A'] == selection])

#      A      B  C   D
# 0  foo    one  0   0
# 2  foo    two  2   4
# 4  foo    two  4   8
# 6  foo    one  6  12
# 7  foo  three  7  14

Or I can easily change what is stored in "selection" to get "bar" as well. Now, what if I want to select everything in column A, but I do not want to change anything else other than what is stored in "selection"?

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
Kin
  • 11
  • 1
  • What data structure(s) are you using? What is `df`? – Simon Jul 26 '18 at 15:18
  • probably turn `selection` into a list and use `df['A'].isin(selection)` as your mask. That way you can easily extend the masking to any number of values. If you need them all you'd do `selection = df['A'].unique()`, or simply don't mask the `DataFrame` to begin with. – ALollz Jul 26 '18 at 15:20
  • sorry, i'm using Pandas dataframe. df is just the dataframe. – Kin Jul 26 '18 at 15:20

0 Answers0