How to go through all the data of a DF making calculation and return the value for it?

Asked

Viewed 467 times

2

dfI am with DF after some calculations, but I am not able to pass the data to an account, every time gives this error:

The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Code:

#biblilotecas
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_excel('Semana.xlsx') #leitura do excel
df1 = pd.read_excel('Semana1.xlsx') #leitura do excel
df2 = pd.read_excel('Semana2.xlsx') #leitura do excel
df3 = pd.read_excel('Semana3.xlsx') #leitura do excel

df = df + df1 + df2 + df3
df = df.T #girando tabela
df = df.drop(['Dia'], axis=0)
df = df.T #girando tabela
df = df // 4
df.columns = ['1', '2' ,'3','4','5','6','7']

Parte da conta: 

Lamb = ' aqui tem que recebe o valor de cada posiçao ' 
u = 7
Caixas = 22

for lamb in lamb:
    while(lamb/(Caixas*u) >= 0.9):
        Caixas += 1

while(lamb/(Caixas*u) <= 0.5):
    Caixas -= 1
else:
    print('Resultados')
    F = lamb/(Caixas*u)
    F = F * 100
    print('==========')
    print(lamb, '/', Caixas,'*',u)
    print('Numero de caixas recomendável e:',Caixas)
    print('Caixas ficam ocupados' ,"%.0f" % F , "%")
  • can you give an example of how this data is? they have header? are of the same type?

  • What is lamb? lamb and Lamb are the same thing or are different variables?

  • Cara has a lot wrong with your code, I recommend you go back a little and study repetition structure, conditionals, data structure and so of the continuation of this project

  • the Amb are repeated because it was trying another way that did not work.. Lamb = the values of DF u = 7 Boxes = 22 while(Lamb/(Boxesu) >= 0.9): print("addition of boxes") Boxes += 1 print(Boxes) Else: print("Reduction of boxes") while(Lamb/(Boxesu) <= 0.5): Boxes -= 1 print(Boxes) Else: print('Results') F = Lamb/(Boxesu) F = F * 100 print('==========') print(Lamb, '/', Boxes,'',u) print('Recommended box number and:'Boxes) print('Boxes get busy' ,"%.0f" %F , "%")

  • I want all DF data to pass through the account, then account ira returns a value and this value will be in DF

1 answer

1

See if this is what you want, it removes 30 from the current value. The "secret" is in the lambda that you put the expression you want or access to a function with return (2nd Example).

"for" accesses column by column and returns the value

#Instalar pacote
#pip install pandas
import pandas as pd

lista_valores = [
                 [74,80,90,0,67,68,73]
                ,[91,88,93,97,71,90,87]
                ,[97,93,102,110,83,97,95]
                ,[110,110,115,154,95,111,101]
                ,[125,144,121,177,97,110,86]
                ]

df = pd.DataFrame(lista_valores, columns = ['1', '2','3','4','5','6','7'])

print('=== Dataframe Original ===')
print(df)
print()

#For das colunas do dataframe
for x in df.columns:
    print('Acessando a coluna:',x)
    #Acessa o campo do dataframe
    #Remover o valor de cada campo
    df[x] = df[x].apply(lambda x: x-30)



print('\n=== Dataframe Alterado coluna a coluna')
print(df)

Using a function to return odd or even for example.

#Instalar pacote
#pip install pandas
import pandas as pd

lista_valores = [
                 [74,80,90,0,67,68,73]
                ,[91,88,93,97,71,90,87]
                ,[97,93,102,110,83,97,95]
                ,[110,110,115,154,95,111,101]
                ,[125,144,121,177,97,110,86]
                ]

df = pd.DataFrame(lista_valores, columns = ['1', '2','3','4','5','6','7'])

print('=== Dataframe Original ===')
print(df)
print()

def impar_par(valor):
    if(valor%2==0):
        return "par"
    else:
        return "impar"

#For das colunas do dataframe
for x in df.columns:
    print('Acessando a coluna:',x)
    #Acessa o campo do dataframe
    #Remover o valor de cada campo
    df[x] = df[x].apply(lambda x: impar_par(x))



print('\n=== Dataframe Alterado coluna a coluna')
print(df)

Browser other questions tagged

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