我所做的新更改的数据框未打印

原文标题The dataframe with the new changes I made is not printed

def ascendiente ():
   return df[df['Nota final'] >= 11].sort_values(ascending=False,by='Nota final')
def descendiente ():
    return df[df['Nota final'] >= 11].sort_values(ascending=True,by='Nota final')
opcion = int(input("Elija la opción (1) si desea los datos en orden ascendente y la opción (2) si los desea en forma descendente: "))
df['Nota final'] = (df['G1'] + df['G2'] + df['G3']) / 3
if opcion == 1:
  ascendiente()
elif opcion == 2:
  descendiente()

我有一个关于一些学生的一组成绩的数据框,我需要根据用户的决定从最低到最高组织赢得每个科目的学生的成绩,但是当我执行说明并运行代码时,没有任何反应,虽然我在代码中调用函数,它仍然没有显示任何东西。我提前感谢帮助,我也想了解这个问题的原因。我需要代码来告诉我这样的东西enter image description here

原文链接:https://stackoverflow.com//questions/71919636/the-dataframe-with-the-new-changes-i-made-is-not-printed

回复

我来回复
  • nfn的头像
    nfn 评论

    首先,您需要将 df 传递给您的函数。调用函数后,将返回值分配给您的 df:

    import pandas as pd
    def ascendiente (dataframe):
       return dataframe[dataframe['Nota final'] >= 11].sort_values(ascending=False,by='Nota final')
    def descendiente (dataframe):
        return dataframe[dataframe['Nota final'] >= 11].sort_values(ascending=True,by='Nota final')
    opcion = int(input("Elija la opción (1) si desea los datos en orden ascendente y la opción (2) si los desea en forma descendente: "))
    
    #creating dataset(you dont need this part as you already have it)
    data = {'G1': [10, 17, 14, 20], 'G2': [20, 8, 19, 18], 'G3': [10, 6, 19, 18]}  
    df = pd.DataFrame(data)
    
    df['Nota final'] = (df['G1'] + df['G2'] + df['G3']) / 3
    if opcion == 1:
      df = ascendiente(df)
    elif opcion == 2:
      df = descendiente(df)
    
    print(df)  
    

    结果:enter image description here

    2年前 0条评论