I have a dataframe A with several columns that is the result of a merge of two dataframes. After the merge, I need to update the values in the answer column in A based on the values in other columns.
Pseudo code:
for all agreements in A :
if the length of the value in the year column in A is less than four:
update string in answer column of A without the value from year column
else:
update string in answer column of A with the value from year column
What I am trying:
for row in A.itertuples() :
if len(str(A.Year)) < 4 :
row.answer = 'Status on ' + row.Name + ' is ' + row.Status
else :
row.answer = 'Status on ' + row.Name + ' ' + str(row.Year) + ' is ' + row.Status
I get an error AttributeError: can't set attribute
Any suggestions?
Since there is no example data, I couldn't tell why the code gives you the error. But you can try using apply()
def generate_answer(row):
if len(str(row.Year)) < 4 :
return ('Status on ' + row.Name + ' is ' + row.Status)
else :
return ('Status on ' + row.Name + ' ' + str(row.Year) + ' is ' + row.Status)
A['answer'] = A.apply(generate_answer, axis=1)