-2

Overview: I have a Pandas Dataframe with 6 columns and the Output is needed in a text file as given below.

Input:

Pandas_Input_DF

The output is needed in a Text file as shown below:

Intercept = 1.078
(DIVISION = '3' )*0.448
(DIVISION = '2' )*-0.024
(DIVISION IN ('1','4','5' ))*-0.002
(NEWLSGolfIN  IN ('1','Y' ))*0.093
('001'  LE ppseg30  LE '010' )*0.056
('011'  LE ppseg30  LE '020' )*0.043
(ppseg30 = '21' )*0.052
Mrinal Roy
  • 969
  • 7
  • 12
  • 1
    Please share the input as a text block too instead of an image, so it's easier for others to recreate. From your question, I guess you need the logic to transform your input to the desired output? Not saving [Python, Pandas : write content of DataFrame into text File Ask](https://stackoverflow.com/a/31247247/8840245), right? – Mrinal Roy Jul 03 '20 at 18:19
  • Yes, I need the logic for creating the Output as text file as mentioned above. Thanks for the suggestion, from next time I will create a text block instead of image – Rishabh Kumar Jul 07 '20 at 06:59
  • Please edit your question to include the Dataframe as text. That makes it way easier for us to help with debugging. – Joooeey Jul 09 '20 at 18:54

1 Answers1

0

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)
Joooeey
  • 3,394
  • 1
  • 35
  • 49
  • Hi @joooeey, Thanks for checking over this. I tried run your code but in the Ouput I am getting rows even for the null values for "EQVAL". Output is as shown below: (Intercept = 'nan)*1.078 (DIVISION = '3')*0.44799999999999995 (DIVISION = '2')*-0.024 (DIVISION = '1')*-0.002 (DIVISION = '5')*-0.002 (DIVISION = '4')*-0.002 (NEWLSGolfIN = 'nan)*0.09300000000000001 (ppseg30 = 'nan)*0.055999999999999994 (ppseg30 = 'nan)*0.043 (ppseg30 = '21')*0.052000000000000005 – Rishabh Kumar Jul 08 '20 at 10:31
  • Hi @Joooeey , Did you have a chance to further look into the code? – Rishabh Kumar Jul 08 '20 at 17:37
  • Looks like that field isn't empty but it is `np.nan`. `bool(np.nan) == True`, that's why my code doesn't work. Better use Pandas' own `pd.notna` function for detecting non-empty cells: `if pd.notna(row_series['EQVAL']):`. https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.notna.html#pandas.notna – Joooeey Jul 09 '20 at 18:53