[Tutor] code explanation

Alan Gauld alan.gauld at yahoo.co.uk
Thu Nov 25 03:52:01 EST 2021


On 23/11/2021 11:51, Tariq Khasiri wrote:
> ```
> data = [[col, df[col].nunique()] for col in
> df.columns.difference(["Reviews"])]
> uniques = pd.DataFrame(data=data, columns=["columns", "num of unique
> values"])
> 
> ```
> 
> The first line is explaining the dataframe ( df) unique number of
> elements in a column but i dont understand the the second half of the first
> line of coding neither the second line of code. Can anyone help ?

I'm not sure what aspect you don't understand so I'll start by stating
that the first two lines of code are a single line of Python split for
readability. I'll simplify it so that it can be seen on a single line:

func1 = df[0].nunique  #0 is arbitrary, assumes all items of same type
func2 = df.columns.difference

data = [[cl, func()] for cl in func2(["Reviews"])]

So the line is a list comprehension making use of two functions.

If that is not what was confusing you please repost with more specifics.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list