Home > Mobile >  Pandas Sorting Filtering
Pandas Sorting Filtering

Time:11-16

My question was deleted, can't undo it.

CodePudding user response:

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
  • Related