0

Reading an imported dataset, I am trying to divide two string columns in the dataset:

df['Cost'] = (df['Paid']/df['Items'])

I also tried:

df['Cost'] = df['Paid']/df['Items']

and

df['Cost'] = df['Paid'].div(df['Items'])

The paid column has decimal points and some values are 00.00. Can you get a mean value on a sting column? Will I have issues using this column as string?

I get the following error

error: Unsupported operand type(s) for /: 'str' and 'str'

when trying to change type to float, so trying to work with string. This is the error when trying to change to float:

ValueError: could not convert string to float: '(94.00)')

Timus
  • 10,974
  • 5
  • 14
  • 28
user18385629
  • 21
  • 1
  • 3
  • 1
    Please provide a sample of the dataframe (as text!): `df.head().to_dict('list')` – mozway Apr 03 '22 at 17:20
  • Given the question in the title - _Can you divide two string columns of numbers in python_, I am curious what is the expected result of dividing two strings? – buran Apr 03 '22 at 17:53
  • [How to make good reproducible pandas examples](https://stackoverflow.com/q/20109391/4046632) – buran Apr 03 '22 at 17:54
  • Why your strings have braces - `'(94.00)'`? – buran Apr 03 '22 at 17:56

1 Answers1

0

first make sure that your columns contain strings of numbers without strange characters, for example the last error that you indicate can be seen that there is '(' in your strings so you will not be able to convert to float

My advice is to clean the columns with the following command:

df.Paid.str.extract('(\d+)')

What this function will do is that it will only leave you the digits it finds in your column (if you need decimals you also have to create a custom regex)

Once you have everything transformed you can do the division, I hope it helps you.

Ncgomez17
  • 68
  • 6