I need to select the last Dataframe value to calculate the value of the next row in the same column

Asked

Viewed 1,220 times

1

I need to select the last value of the column "Season Minimum" to calculate the value of "Mint", name I gave the variable of the same column, currently my line of codes is like this:

import numpy as np
import pandas as pd

#DATABASE
data = {'Jogos':[1, 2, 3, 4], 
    'Placar':[12, 24, 10, 24],
    'Mínimo da Temporada':[12, 12, 10, 10],
    'Máximo da Temporada':[12, 24, 24, 24],
    'Quebra recorde mín':[0, 0, 1, 1],
    'Quebra recorde max':[0, 1, 1, 1]}

df = pd.DataFrame(data)

j = input("Número de Jogos:")
p = input("Qual foi o placar?:")

mint = 0
if int(p) < :
    mint = p


new_row = {'Jogos':j, 'Placar': p, 'Mínimo da Temporada': mint, 'Máximo da Temporada': maxt, 'Quebra recorde mín': qrmin, 'Quebra recorde max': qrmax,}

df = df.append(new_row, ignore_index=True)

print(df)

Sorry for the possible incorrect language, I’m still learning to program.

  • 1

    Welcome, Gabriel. First of all. remove the image from the post and paste the code so that we can copy, test, edit, etc.; After you paste the code into the question, select it and use the Ctrl+K shortcut to format it properly.

  • Thanks for the help, I still don’t know how to use this site right.

1 answer

2


To select the last element you can use iloc:

df['Mínimo da Temporada'].iloc[-1]

Exit:

10

Edit

To add the value to the Mint variable:

mint = df['Mínimo da Temporada'].iloc[-1]
print(mint)
10
  • And to set the value of the variable as that same value, the last of this column of Dataframe, as I would? Mint = df['Mínimo da Temporada]?

  • 1

    Hello Gabriel, good morning! I updated the answer with this new doubt. Hug!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.