将字符串参数转换为Pandas对象

下面,我有一个函数,希望将参数“标签”转换为数据框列/标签对象。在该函数中,参数“标签”是字符串“酒精”。通过参数标签(字符串“ alcohol”)在函数中访问后,我需要将其用作数据框列/标签的名称。数据框列也称为“酒精”。诸如df.label.median()之类的调用应等效于df.alcohol.median(),其中酒精是数据框中的实际列。

import pandas as pd
df = pd.read_csv('winequality-red.csv', sep=';')

def mean_quality_rating(df, label):
    median_label = df.label.median() #should evaluate as df.alcohol.median()
    for i, the_label in enumerate(df.label):
        if the_label >= median_label:
            df.loc[i, label] = 'high'
        else:
            df.loc[i, label] = 'low'
    return df.groupby(label).quality.mean()

mean_quality_rating(df, 'alcohol')
评论
  • ~佑瞄瞄
    ~佑瞄瞄 回复

    尝试:

    def mean_quality_rating(df, label):
        median_label = df[label]median() #should evaluate as df.alcohol.median()
        for i, the_label in enumerate(df[label]):
            if the_label >= median_label:
                df.loc[i, label] = 'high'
            else:
                df.loc[i, label] = 'low'
        return df.groupby(label).quality.mean()
    
    mean_quality_rating(df, 'alcohol')
    

    您不能将点符号与变量一起使用。