Reputation: 585
I have created a loop to manipulate a dataframe from a query.
contas = ["Margem Bruta","Faturamento", "Resultado Bruto", "Resultado Líquido", "Despesas Operacionais", "Despesas Vendas"]
results = []
for i in contas:
The result is a dataframe like this:
Year Period y yhat Contas Resultado
0 2017 1 23.818044 23.818033 Faturamento
1 2017 2 20.275252 20.275264 Faturamento
2 2017 3 22.347278 22.347281 Faturamento
3 2017 4 23.837490 23.837491 Faturamento
4 2017 5 23.460605 23.460606 Faturamento
5 2017 6 22.307115 22.307115 Faturamento
6 2017 7 23.643994 23.643991 Faturamento
7 2017 8 23.791720 23.791714 Faturamento
8 2017 9 23.643933 23.643927 Faturamento
9 2017 10 20.771269 20.771262 Faturamento
10 2017 11 21.317947 21.317948 Faturamento
11 2017 12 22.361570 22.361584 Faturamento
12 2018 1 24.336259 24.336270 Faturamento
13 2018 2 19.926928 19.926916 Faturamento
14 2018 3 22.714901 22.714898 Faturamento
15 2018 4 23.605119 23.605118 Faturamento
16 2018 5 23.653298 23.653297 Faturamento
17 2018 6 23.052182 23.052181 Faturamento
18 2018 7 24.377920 24.377923 Faturamento
19 2018 8 24.576733 24.576738 Faturamento
20 2018 9 24.376775 24.376781 Faturamento
21 2018 10 21.256970 21.256976 Faturamento
22 2018 11 21.969202 21.969200 Faturamento
23 2018 12 22.970637 22.970623 Faturamento
25 2019 1 0.000000 19.337573 0
[125 rows x 5 columns]
My problem is:
How can I replace the last 0 value of 'Contas Resultado' Column using the loop list ?
I tried this:
result = result['Contas Resultado'].replace({'0': [i]}, inplace = True)
But I didn't get the right output :
0 1 \
Contas Resultado Faturamento Faturamento
Contas Resultado Resultado Bruto Resultado Bruto
Contas Resultado Resultado Líquido Resultado Líquido
Contas Resultado Despesas Operacionais Despesas Operacionais
Contas Resultado Despesas Vendas Despesas Vendas
I just want to change all the lines that "contas resultado' is zero.
25 2019 1 0.000000 19.337573 0
to the corresponding value of the loop list
25 2019 1 0.000000 19.337573 Faturamento
Upvotes: 0
Views: 121
Reputation: 491
try the following
idx = result['Contas Resultado'] == 0
result.loc[idx, 'Contas Resultado'] = 'Faturamento'
Upvotes: 2