【问题标题】:Pandas Sorting Filtering熊猫排序过滤
【发布时间】:2022-11-16 04:33:33
【问题描述】:

我需要你的帮助在 Python 上使用 Pandas。

我必须通过显示该列的值小于该列的平均值的所有行来过滤具有最唯一值的数字列上的数据集行。

我该如何解决这个问题?

def pandasFilter(filePath):
    
    df1 = pd.read_csv(filePath) #creating a dataframe
    nd = df1.select_dtypes("number") #only selecting the numeric columns
    v_i = nd.nunique() #sorting unique values of the numeric columns

是我的脚步

【问题讨论】:

    标签: python pandas csv


    【解决方案1】:

    这是你想做的吗?

    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
    

    【讨论】:

    • 谢谢,它有效!
    • 这也可以不使用熊猫吗?仅使用 csv 导入?
    猜你喜欢
    • 2018-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-22
    • 2017-05-22
    • 2018-08-09
    • 2020-01-13
    • 2021-08-20
    相关资源
    最近更新 更多