This can be done efficiently by writing a function to convert each row to a string and then applying it to the rows of the DataFrame with DataFrame.apply
. The keyword argument axis=1
specifies that the function should be applied to the rows, not the columns.
The conversion itself consists of figuring out what kind of comparison it is and then constructing the string with an f-String.
def row_to_string(row_series):
"""Convert a DataFrame row to the correct string"""
if row_series['EQVAL']: # if EQVAL field is not empty
return f"({row_series['VNAME']} = '{row_series['EQVAL']})*{row_series['ESTIMATE']}"
# ... add the if statements and return statements for the other three types of comparisons yourself!
text_lines = dataframe.apply(row_to_string, axis=1)
text_lines.to_csv('text_file.txt', index=None, header=None)