Get unique values from Pandas series based on string

I’ll save a lot of code if I can use a server module like this to return an array of unique column contents from a media object transformed into a pandas df. I want to pass the column name to the server function and have it return an array of that column’s unique values: file=app_tables.files.get(filetype=‘MARSYS’)

THIS definitely does not work, but I think it conveys the concept. I want to build a string ‘ask’ but use the value of that variable as the pd.series query:

def get_column(col):
  df=pd.read_csv(io.BytesIO(file.get_bytes()), names=["industry", "top_level", "category"])
  ask = "df." + col + ".unique()"
  array = pd.Series($ask)

If I understand correctly, this is a Pandas question.

If so, you can do something like:

unique_vals=df[col].unique()

If col is a string that indicates a column in your DataFrame, and you would like a list of unique values from that column, then this should work.

Also, you can format your code on the forum by wrapping it in backticks with the word Python. This makes it easier for folks to read (and therefore easier to provide assistance). For example,

```python

print('this will be syntax highlighted')

```
3 Likes