0

If I have a dataframe df and want to access the unique values of ID, I can do something like this.

UniqueFactor = df.ID.unique()

But how can I convert this into a function in order to access different variables? I tried this, but it doesn't work

def separate_by_factor(df, factor):

    # Separating the readings by given factor
    UniqueFactor = df.factor.unique()

separate_by_factor('ID')

And it shouldn't, because I'm passing a string as a variable name. How can I get around this?

I don't know how I can better word the question, sorry for being too vague.

J. Dionisio
  • 159
  • 1
  • 13

1 Answers1

1

When you create a DataFrame, every column that is a valid identifier it's treated as an attribute. To access a column based on its name (like in your example), you need to use df[factor].unique().

crissal
  • 2,547
  • 7
  • 25
  • This works, thank you :) However, do you know how I can do what I was trying to do? Actually passing an unknown variable like that? It could be useful in other situations – J. Dionisio Jan 28 '21 at 12:33
  • Actually, there are no scenarios where it's useful (it's useful only when if you already know your column name and access it, like `df.ID`) – crissal Jan 28 '21 at 12:45
  • I believe it could be, if you are expecting a user input. Not for this particular situation, but reading a string and associating it to a variable seems like it could be of use – J. Dionisio Jan 28 '21 at 13:02
  • 1
    Then I suggest you to check out [this thread](https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables) and [this too](https://stackoverflow.com/questions/8028708/dynamically-set-local-variable); my suggestion is to use dictionaries to do this with variables, and square-brackets access with dataframes – crissal Jan 28 '21 at 13:27
  • thank you for the links! getattr function seems to do the trick as well! – J. Dionisio Jan 28 '21 at 13:44