0

This isn't exactly a question about how to find all unique entries in a column of a dataframe, since I know how I'd do that:

import pandas as pd

df = pd.read_csv('test.txt',delim_whitespace=True)

for key in list(df.keys()):
    uni = set(df[key])

What this is really about, is how to do it with pandas' own methods/functions dynamically and this strange syntax that I can't understand why anyone would use it:

In [101]: list(df.keys())
Out[101]: ['id_cliente', 'id_ordine', 'data_ordine', 'id_medium']

With these keys, you can find their unique column values with the following syntax:

In [102]: df.id_cliente.unique()
Out[102]: array(['madinside', 'lisbeth19'], dtype=object)

I can't use this method dynamically like in my iteration above, can I? I can only use it, if I find out the keys first and manually type in the df.NAME.unique() statement, right?

Why is this a thing? Is this method exclusively intended for interactive use from the python console? Is there a native pandas.DataFrame method for determining unique values dynamically?

2 Answers 2

1

You can do it dynamically

df.T.apply(pd.Series.unique,1)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. But could you maybe tell me, if the syntax is actually weird or if I'm just weird? Look at my other comment if you want to know what I mean?
1

Does this work for your df?

unique_stuff = [{col: set(df[col].unique())} for col in df.columns]

edit: actually I don't think you even need a set here. I've removed it below:

unique_stuff  = [{col: df[col].unique().tolist()} for col in df.columns]

1 Comment

Yeah I could do that, it's just that I found it weird that you can access a column of a dataframe by using it's column name string as a code fragment. Doesn't this mean, that when a DataFrame instance is created, the column names are used to create static variables, where the name of the static variables is also the name of the column? Am I the only one who finds this weird? stackoverflow.com/questions/68645/…

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.