0

I have this dataset, which comes from an excel report:

when I use the following code:

import pandas as pd

df = pd.read_excel(r'C:\Users\Charles\Documents\CLS\CLS Python\Dashboard.xlsx')

profit = df['Gross Margin']
sales = df['Total Billing Rate']
df['Net_%'] = profit / sales

it results in:

TypeError: unsupported operand type(s) for /: 'str' and 'int'

I have tried googling and using int and float but cannot get the code to even begin running. What am I doing wrong?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • Welcome to Stack Overflow. Please read [ask] and carefully note that this is **not a discussion forum**. We are **not interested** in your level of expertise - it does not change the answer to the question, is not actually useful in helping write the answer more clearly, and is distracting for people who find the question later with a search engine. We **are** interested in a *specific, clearly asked question*. I have [edit]ed your post to show how it's done - please carefully study it. – Karl Knechtel Jun 01 '22 at 15:25
  • That said, there is still missing information that I cannot provide for you, because only you know it. It is not helpful to tell us "I have tried googling and using int and float" - *What* did you search for? What did you find? What did it suggest doing? What happened when you tried doing that? **How** did you try "using int and float"? What change did you make to the code? What happened as a result? Also, please show textual data [as text, not an image](https://meta.stackoverflow.com/questions/2855510) - that includes `DataFrame`s in your code. – Karl Knechtel Jun 01 '22 at 15:29
  • Finally: please read https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ to learn some skills for tracking down the problem yourself. It seems that you already understood that the `df['Gross Margin']` column has strings in it, and that you want it to contain numbers instead. The next step is to find out why it has strings, *what strings it has*, and what to do about it. – Karl Knechtel Jun 01 '22 at 15:31
  • Yes and that's what I'm asking -- I've done a few hours of research and cannot find how to make this column integers. – CLSolutions Jun 07 '22 at 14:35
  • "I've done a few hours of research and cannot find how to make this column integers." Did you try putting `pandas change type of dataframe column` [into a search engine](https://duckduckgo.com/?q=pandas+change+type+of+dataframe+column)? When you look at those results, what do they tell you to do? What things did you try doing from those suggestions, and how did they fail to solve the problem? – Karl Knechtel Jun 07 '22 at 16:01
  • For example, does https://stackoverflow.com/questions/15891038 answer the question? That was the third result for me. – Karl Knechtel Jun 07 '22 at 16:02

1 Answers1

2

The error means that when you are trying to perform / operation, left hand side of it is str, while right hand side is int, which is not allowed.

The problem is, df['Gross Margin'] seems to include some - entries, which are str, so whole column gets read as str. Probably what you should do is replace - with 0 or some other value like this:

df['Gross Margin'] = df['Gross Margin'].replace('-', 0)
Ach113
  • 1,775
  • 3
  • 18
  • 40
  • I still get the same error -- I understand I did a poor job phrasing the question, but it seems like even when I replace all via 'df.replace['-', 0)' – CLSolutions Jun 07 '22 at 04:22