Home > Blockchain >  The dataframe with the new changes I made is not printed
The dataframe with the new changes I made is not printed

Time:04-20

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()

I have a dataframe about a set of grades of some students and I need to organize the grades of those who won each subject from lowest to highest according to what the user decides, but when I do the instructions and run the code nothing happens, although I am calling the function inside the code, it still doesn't show anything. I appreciate the help in advance and I would also like to understand the reason for this problem. I need the code to show me something like this enter image description here

CodePudding user response:

First of all, you need to pass the df to your function. After calling the function, assign the return value to your 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)  

result: enter image description here

  • Related