1

I want to create a column named 'texto2' that show me different outcomes according to lines of the dataframe (called text_ind). In other words, I want to write a message in case the column 'VERIF' is OK, else I dont want any message. The message will vary according to the line.

DATAFRAME

text_ind = pd.DataFrame({'ISS_2019': [10, 20, 50], 'ISS_2020': [11, 24, 40],
'change_ISS': ['10%', '20%', '-20%'],
'VAR_ISS_REAL': ['crescimento', 'crescimento', 'queda']})

DESIRED DATAFRAME

text_ind = pd.DataFrame({'ISS_2019': [10, 20, 50],'ISS_2020': [11, 24, 40],
'change_ISS': ['10%', '20%', '-20%'], 'VAR_ISS_REAL': ['crescimento', 'crescimento', 'queda'],
'texto2': ['com um crescimento real de 10% em comparao com 2019', 'com um crescimento real de 20% em comparaocom 2019', 'com uma queda real de 20% em comparao com 2019']})

script

text_ind['FLAG'] = pd.notnull(text_ind[text_ind.columns[1:2]]).astype(int)    
text_ind['FLAG2'] = pd.notnull(text_ind[text_ind.columns[2:3]]).astype(int)
text_ind.loc[text_ind['FLAG'] == 1, 'VERIF'] = 'OK'
text_ind.loc[text_ind['FLAG2'] == 1, 'VERIF'] = 'OK'

text_ind['texto2'] = text_ind['VERIF'].apply(lambda x: 'com um(a) {x['VAR_ISS_REAL']} 
real de {x['change_ISS']} em comparação com 2019' if x == 'OK' else '')

invalid syntax error message

aneroid
  • 12,983
  • 3
  • 36
  • 66
Fernanda
  • 313
  • 1
  • 6
  • Your last line says data_ind instead of text_ind. Do you have a dataframe called data_ind? – pakpe May 24 '21 at 18:04
  • Please provide a [mcve] with your input and expected output, and a description of what went wrong with your attempts. See [How to make good pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – G. Anderson May 24 '21 at 18:04
  • 2
    @Scinana we ask that questions include sample data as _text_ in the question, not as a screenshot or link, so that we can reproduce the data to help understand and solve the problem – G. Anderson May 24 '21 at 18:06
  • Welcome to StackOverflow. Use `text_ind.head(10).to_clipboard(excel=False)` to copy the first 10 rows of your dataframe to clipboard (as text) which you can then paste into your question. Read this post on [how to provide a good pandas example](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). Also, [improve the formatting of your code](https://stackoverflow.com/editing-help). – aneroid May 24 '21 at 18:14

2 Answers2

1

Your method is incorrect by using lambda. Use this instead to create texto2 column

texto2 = []
for var_iss, change_iss, verif in zip(text_ind['VAR_ISS_REAL'].values.tolist(), text_ind['change_ISS'].values.tolist(), text_ind['VERIF'].values.tolist()):
  if verif == 'OK':
    texto2.append(f"com um {var_iss} real de {change_iss} em comparação com 2019")
  else:
    texto2.append('')

text_ind['texto2'] = texto2
Prakash Dahal
  • 4,388
  • 2
  • 11
  • 25
  • "Thanks for the feedback! You need at least 15 reputation to cast a vote, but your feedback has been recorded." – Fernanda May 25 '21 at 19:26
0

Things which need fixing:

  • You're trying use f-strings but forgot the f prefix.
  • And if you have single quotes (') inside a string which you've defined with single quotes, then you have to either
    1. escape it with a backslash \' or
    2. use double-quotes for the string and single-quotes for the column names (or vice-versa).
  • You're using apply on a single column VERIF but you're referring to other columns VAR_ISS_REAL and change_ISS

I've kept your .apply mostly intact with these changes:

  • I'm using apply on the whole dataframe
  • with axis=1 so that it process row-by-row (so the x is now the row)
  • (extra newlines for readability)
text_ind['texto2'] = text_ind.apply(
    lambda x: f"com um(a) {x['VAR_ISS_REAL']} real de {x['change_ISS']} em comparao com 2019"
              if x['VERIF'] == 'OK' else '',
    axis=1)

Output:

   ISS_2019  ISS_2020 change_ISS VAR_ISS_REAL  FLAG  FLAG2 VERIF                                                  texto2
0        10        11        10%  crescimento     1      1    OK  com um(a) crescimento real de 10% em comparao com 2019
1        20        24        20%  crescimento     1      1    OK  com um(a) crescimento real de 20% em comparao com 2019
2        50        40       -20%        queda     1      1    OK       com um(a) queda real de -20% em comparao com 2019
aneroid
  • 12,983
  • 3
  • 36
  • 66