Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Python function list_col(df, col). Its first input is a pandas dataframe

ID: 3699176 • Letter: W

Question

Write a Python function list_col(df, col). Its first input is a pandas dataframe. Its second input is a string col that is the name (i.e., label) of one of the columns of the dataframe. Your function should return a Python list that contains every unique entry in the column named colin sorted order.

For example, using the justice-centered database and calling its dataframe scdb as we did in examples list_col(scdb, 'chief') should return the list:

['Burger', 'Rehnquist', 'Roberts', 'Vinson', 'Warren']

You may not use any pandas module methods or dataframe or series methods (other than indexing the dataframe by a col, which technically would be considered a dataframe method).

No error checking is required; you may assume that there is an column with name col and that its entries are all of types that can be sorted together. Also, your function does not have to work well on columns with missing or NaN values. (There are pandas isnull() and notnull() series methods that we could use to weed out the missing values, but our goal is to make this a lab that can be done quickly, so we're leaving out that fiddly bit.)

Explanation / Answer

Code:

def list_col(df, col):

a = df[col]

z = [i for i in a] #create a list z with entries from the column 'col' of the dataframe

z = unique(z) # calls the user defined function

return sorted(z) #returns sorted list

def unique(l): #funtion takes list as input argument

d = {} #create an empty dictionary

for i in l:

try:

d[i]+=1

except:

d[i]=1 #Add all unique values to dictionaly from list 'l'

return d.keys() #return all keys of the dictionary

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote