1

i have:

df['tabela_qar.pergunta'].value_counts()

enter image description here

df['tabela_qar.resposta'].value_counts()

enter image description here

I need to change the values ​​in tabela_qar.resposta from some conditions.

Example:

If tabela_qar.pergunta= x & tabela_qar.resposta = y: tabela_qar.resposta changes value to z

Tried:

df.loc[(df['tabela_qar.resposta']=='NÃO') & (df['tabela_qar.pergunta']=='POSSUI GARAGEM NO LOCAL DE TRABALHO')] = df['tabela_qar.resposta'].str.replace('NÃO','GARAGEM TRABALHO')

I tried in several ways, but I can't do it. Someone help me?

LpCoutinho
  • 37
  • 4
  • 1
    * [Please do not post data or sample code as images](https://meta.stackoverflow.com/q/285551). * Please provide sample data in a [reproducible way](https://stackoverflow.com/questions/20109391). Otherwise people won't be able to test. * Please exemplify the expected output explicitly. – Bill Huang Nov 28 '20 at 20:57

2 Answers2

0
In [6]: np.random.seed(10)
   ...: df = pd.DataFrame({"tabela_qar.pergunta" : list("amirsaliim"),
   ...:                    "tabela_qar.resposta" :  np.random.randint(1, 5, 10)
   ...:                   })
   ...: df
Out[6]: 
  tabela_qar.pergunta  tabela_qar.resposta
0                   a                    2
1                   m                    2
2                   i                    1
3                   r                    4
4                   s                    1
5                   a                    2
6                   l                    4
7                   i                    1
8                   i                    2
9                   m                    2

In [7]: df.loc[(df['tabela_qar.pergunta'] == "a") & (df['tabela_qar.resposta'] == 2), "tabela_qar.resposta"] = 999
   ...: df
Out[7]: 
  tabela_qar.pergunta  tabela_qar.resposta
0                   a                  999
1                   m                    2
2                   i                    1
3                   r                    4
4                   s                    1
5                   a                  999
6                   l                    4
7                   i                    1
8                   i                    2
9                   m                    2

Amir saleem
  • 1,404
  • 1
  • 8
  • 11
0

I would recommend using df.query() within an np.where(condition = df.query(), value if true, value if false)

df['tabela_qar.pergunta] = np.where(condition, df['tabela_qar.pergunta].replace() if true, df['tabela_qar.pergunta] if false)

Anish
  • 31
  • 4