0

I use a dataframe (NewList) to calculate a variable (NPQ), and then I would like to save this variable (which is a list) into a column in the original dataframe. I get an error.

def NPQ_calculation(NewList):      #this is the dataframe      
   Fm_prime = NewList.iloc[0:7, 1] #2nd column in dataframe
   NPQ = []
   NPQ.append(Fm/Fm_prime - 1)     #Fm is the 4th column in dataframe
   return NPQ                      # this is the variable I want to add

# call function for NPQ calculation
NPQ = NPQ_calculation(NewList)
###PROBLEM 
NewList['NPQ']= NPQ

This is the error message I get:

ValueError: Length of values does not match length of index

This is the original data frame to which I would like to add the new column (NPQ) [![I use the 2nd and 4th column to make the calculation of NPQ and would like to add the result in the last column][2]][2])

Thank you very much in advance enter image description here

  • Does this answer your question? [add columns different length pandas](https://stackoverflow.com/questions/27126511/add-columns-different-length-pandas) – Collin Heist Oct 26 '20 at 16:33
  • @Collin Heist No, because if I try to convert NPQ into Dataframe and then concatenate to the previous dataframe, I get NaN values NPQ = NPQ_calculation(NewList) NPQ=pd.Series(NPQ) NewList = pd.concat([NewList,NPQ], axis=1) – Martina Lazzarin Oct 26 '20 at 17:21
  • The whole column is `NaN`, or just the unmatched values? Because the error you are getting means that the length of `NPQ` is not the same as the number of rows in `NewList`, so you will get `NaN` – Collin Heist Oct 26 '20 at 17:27
  • All columns and the output NPQ becomes a row instead of a column, and it gets a different name than NPQ. I added a screenshot – Martina Lazzarin Oct 26 '20 at 17:37
  • Make the NPQ 'column' a `DataFrame` object, not a `Series` object. So `NPQ = pd.DataFrame({'NPQ': NPQ})` and then do `NewList = pd.concat([NewList, NPQ], axis=1)`. – Collin Heist Oct 26 '20 at 17:48

1 Answers1

0

Make NPQ a DataFrame and then append it to the original frame such as:

NPQ_data = NPQ_calculation(NewList)

NPQ_df = pd.DataFrame({'NPQ': NPQ_DATA})

NewList = pd.concat([NewList, NPQ_df], axis=1)

Note: Posting answer from comments so the question can be marked as resolved. Still a probable duplicate of this post.

Collin Heist
  • 1,962
  • 1
  • 10
  • 21
  • 1
    Thank you for your help @Collin Heist, but I still get a strange output. Now the output is no longer visible as row, but the variable NPQ still gets NaN. I really don't understand what am I omitting. Again, I share picture. Thank you again – Martina Lazzarin Oct 26 '20 at 17:58
  • It looks to me like you are just appending a single value to `NPQ` within `NPQ_calculation()`, will you verify if that's the case (try printing the value)? If the one value is a list, try `NPQ.extend()` instead of `append()` – Collin Heist Oct 26 '20 at 18:03