1

How to convert string to float? Here is my code, I tried using dtype and astype method:

for f in files:
    data = pd.read_csv(os.path.join(path, f), sep=";", dtype={"Unnamed: 5":float})
    data=data.drop(data.index[:4])
    df = df.append(data)
for x in df:
    print(x)
    if x == "Unnamed: 0":
        feeName = df["Unnamed: 0"]
        generalLedger = df["Unnamed: 3"]
        amount = df["Unnamed: 5"].astype(np.float64)
        print(feeName, generalLedger, amount)
      
index = 0

sumManagementFee = 0

for x in feeName:
    if x == "Management Fee":
        rows = float(amount.iloc[index])
        sumManagementFee += rows

The error is:

File "pandas/_libs/parsers.pyx", line 756, in pandas._libs.parsers.TextReader.read
File "pandas/_libs/parsers.pyx", line 771, in pandas._libs.parsers.TextReader._read_low_memory
File "pandas/_libs/parsers.pyx", line 850, in pandas._libs.parsers.TextReader._read_rows
File "pandas/_libs/parsers.pyx", line 982, in pandas._libs.parsers.TextReader._convert_column_data
File "pandas/_libs/parsers.pyx", line 1056, in pandas._libs.parsers.TextReader._convert_tokens
ValueError: could not convert string to float: 'Amount'

4
  • What's the full error message? Commented Jul 16, 2021 at 9:48
  • data = self._reader.read(nrows) File "pandas/_libs/parsers.pyx", line 756, in pandas._libs.parsers.TextReader.read File "pandas/_libs/parsers.pyx", line 771, in pandas._libs.parsers.TextReader._read_low_memory File "pandas/_libs/parsers.pyx", line 850, in pandas._libs.parsers.TextReader._read_rows File "pandas/_libs/parsers.pyx", line 982, in pandas._libs.parsers.TextReader._convert_column_data File "pandas/_libs/parsers.pyx", line 1056, in pandas._libs.parsers.TextReader._convert_tokens ValueError: could not convert string to float: 'Amount' Commented Jul 16, 2021 at 9:50
  • Looks like the Unnamed: 5 column contains the literal string "Amount" which can't be parsed as float, yet {"Unnamed: 5":float} in your code says that all values of that columns are floats. Commented Jul 16, 2021 at 9:53
  • How can I then use data as floats, except for the Amount? Commented Jul 16, 2021 at 9:54

1 Answer 1

2

So, your error shows that there is an 'Amount' string in the column "Unnamed: 5" that you're trying to convert.

You can remove it manually, or you can try using pd.to_numeric:

amount = pd.to_numeric(df["Unnamed: 5"], errors='coerce')

Instead of

amount = df["Unnamed: 5"].astype(np.float64)

Specifying argument errors='coerce' will replace all invalid values with NaN

Sign up to request clarification or add additional context in comments.

3 Comments

Actually it didn't. Now amount is full of nan values
Check your amount column. It is full of 'nan' values because the values stored there might be invalid (as the error says, e.g you're having 'Amount' value in that column, which is string)
Amount is in dataframe but not in amount. amount has a strings as '2014.12'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.