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.

1
  • 1
    You can access columns through string using [] operator like such df[factor].unique() Commented Jan 28, 2021 at 12:29

1 Answer 1

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().

Sign up to request clarification or add additional context in comments.

5 Comments

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
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)
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
Then I suggest you to check out this thread and this too; my suggestion is to use dictionaries to do this with variables, and square-brackets access with dataframes
thank you for the links! getattr function seems to do the trick as well!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.