3

I am trying to figure out how to create a new column for my df and cant seem to get it to work with what I have tried.

I have tried using


loans_df.insert("Debt_Ratio",["MonthlyDebt"*12/"Income"])

but I keep getting an error stating unsupported operand type.

BTW I am calculating the new column using already predefined columns in my df

loans_df.insert("Debt_Ratio",["MonthlyDebt"*12/"Income"])

My expected results would be that this new column is inserted into the df with the specific calculation defining it.

Hope this all makes sense!

Gonçalo Peres
  • 11,752
  • 3
  • 54
  • 83
codeD
  • 41
  • 2
  • 4
    IIUC, you need df["Debt_Ratio"] = df["MonthlyDebt"] * 12/df["Income"] – Vaishali Apr 29 '19 at 22:49
  • Thank you, I figured it was something I was missing in regards to using insert instead of making the parameters equal the new column. Is there anyway I can mark this as solved? Can't see anywhere on here for the acceptance of the answer – codeD Apr 30 '19 at 15:17
  • It’s a duplicate so only two options; either you delete the question or I write a solution as an answer:) – Vaishali Apr 30 '19 at 15:29
  • Oh no did I ask a question that was already asked ?! If so I will delete the question once I get home from work :o – codeD Apr 30 '19 at 16:56
  • Yup, not an exact duplicate but there are many variations of this on SO already. That’s the reason i answered as a comment – Vaishali Apr 30 '19 at 17:38
  • Well great..I didn't want to be that guy. I will make sure to take it down after I get home – codeD Apr 30 '19 at 18:07
  • Possible duplicate of [pandas create new column based on values from other columns](https://stackoverflow.com/questions/26886653/pandas-create-new-column-based-on-values-from-other-columns) – PV8 Jun 17 '19 at 12:03

1 Answers1

0

Considering that dataframe is loans_df and that the column you want to create is named Debt_Ratio, the following will do the work

loans_df['Debt_Ratio'] = loans_df['MonthlyDebt'] * 12/loans_df['Income'] 
Gonçalo Peres
  • 11,752
  • 3
  • 54
  • 83