4

I am trying to create a DataFrame from a simple if statement result with no success. Could you show me the right method, please? This is what I have so far but the value of discrep is not added to the DataFrame.

discrepancy_value=round(system_availability.iloc[0,0]-data_av.iloc[0,0],2)
discrep=[]
if discrepancy_value>1:
    discrep=discrepancy_value
else:
    discrep=r'Discrepancy is not significant'
discrepancy=pd.DataFrame()
discrepancy['Discrepancy']=discrep
RhysD
  • 1,597
  • 2
  • 17
  • 30
blooregard
  • 59
  • 4

3 Answers3

4

Your problem is, that you are trying to insert a single value in the dataframe. The dataframe needs lists, not values. What you should be doing is:

discrep=[]
if discrepancy_value>1:
    discrep.append(discrepancy_value)
else:
    discrep.append(r'Discrepancy is not significant')
discrepancy=pd.DataFrame()
discrepancy['Discrepancy']=discrep
LeoE
  • 2,054
  • 1
  • 11
  • 29
0

On one line:

discrepancy = pd.DataFrame({'Discrepancy': [discrepancy_value if discrepancy_value > 1 else r'Discrepancy is not significant']})
Lukas Thaler
  • 2,672
  • 5
  • 15
  • 31
0

You are trying to set a column on an empty dataset with 0 rows. If there would be already rows in the dataframe the following would add the same value to all rows:

discrepancy['Discrepancy']=discrep

But because there are no rows in the dataframe, the column is not added to any row.

You could append a new row with the column value like this:

discrepancy.append([{'Discrepancy': discrep}])

Or add the row already when you create the dataframe

discrepancy=pd.DataFrame([{'Discrepancy': discrep}])
mjspier
  • 6,386
  • 5
  • 33
  • 43