Pandas Sorting Filtering

Question:

My question was deleted, can’t undo it.

Asked By: otto334

||

Answers:

Is this what you want to do?

def pandasFilter(filePath):
    df1 = pd.read_csv(filePath) #creating a dataframe
    nd = df1.select_dtypes("number") #only selecting the numeric columns
    v_i = pd.DataFrame(nd.nunique()).reset_index() #show amount of unique values
    col=v_i[v_i[0]==v_i[0].max()]["index"] #target column label with the highest amount of unique values
    if len(col)==1: #if there is only one column with the highest amount of unique values
        col=col.squeeze()
        colmean=df[col].mean() #get column mean
        results=df[df[col]<colmean] #queried results
    else: #if there are multiple columns with the highest amount of unique values
        results=pd.DataFrame() #return empty dataframe
    return results
Answered By: Toby
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.