How do I convert '$ -' from a string to a float using pandas?

0

Issue

I have some financial information that I’ve kept on an excel document for a while and I’d like to run some python code on it, but I’m having some issues converting the objects types to floats. The problem seems to be the ‘$ -‘

This is how the data looks when loaded in:

import pandas as pd
dfData = {'Item': ['Product 1','Product 2','Product 3'],
        'Cost': [14.87,'-9.47','$ -']
       }
df = pd.DataFrame(dfData,columns=['Item','Cost'])
df
         Item   Cost
0   Product 1   14.87
1   Product 2   -9.47
2   Product 3   $ -

I’ve tried:

df['Cost'] = df['Cost'].str.replace('$','').str.replace(' ','').astype('float')

…as well as other similar str.replace commands, but I keep getting the following error:

ValueError: could not convert string to float: ''

This is my first stack overflow post, so go easy on me! I have looked all over for a solution, but for some reason can’t find one addressing this specific problem. I can’t replace the ‘-‘ either, because row 1 has it indicating a negative value.

Solution

You don’t need to chain str.replace, you can just use replace:

df['Cost'] = df['Cost'].replace({'\$': '', '-': '-0'}, regex=True).astype(float)
print(df)

# Output
        Item   Cost
0  Product 1  14.87
1  Product 2  -9.47
2  Product 3  -0.00

Answered By – Corralien

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More