熊猫数据框中的语法无效

时间:2020-02-24 15:08:48

标签: python pandas dataframe

我试图创建一个列'mdiff',计算'POP ...'列的最大值和最小值之间的差。 它给了我一个语法错误,其中:res = df1.nlargest(1)(我试图在这里获取最大值)。我不确定这里有什么问题吗? 数据是具有列CTYNAME, POPOPESTIMATE2010,..2011,..2012,..2013,..2014..2015.

的数据框
def maxdiff():
    rows =['POPESTIMATE2010',
    'POPESTIMATE2011',
    'POPESTIMATE2012',
    'POPESTIMATE2013',
    'POPESTIMATE2014',
    'POPESTIMATE2015']
    df1=df.where(df['SUMLEV']==50).set_index(['CTYNAME']).dropna()
    df1['mdiff']=df1.apply(lambda x: abs(np.max(x[rows]-np.(min(x[rows])), 
    axis=1)
    dmax=df1.nlargest(1,'mdiff')   

    return dmax

1 个答案:

答案 0 :(得分:0)

您在lambda表达式中缺少右括号:

def maxdiff():
    rows = [
        "POPESTIMATE2010",
        "POPESTIMATE2011",
        "POPESTIMATE2012",
        "POPESTIMATE2013",
        "POPESTIMATE2014",
        "POPESTIMATE2015",
    ]
    df1 = (
        df.where(df["SUMLEV"] == 50)
        .set_index(["CTYNAME"])
        .dropna()
    )
    df1["mdiff"] = df1.apply(
        lambda x: abs(np.max(x[rows] - np.min[rows])),  # <-- One more `)` here!
        axis=1
    )
    dmax = df1.nlargest(1, "mdiff")

    return dmax