-1

I have a series of very large number having datatype as string. I want to change the datatype to float and then represent the number in scientific notation. The array, which is basically a dataframe column, looks like:

x = [1,784,006,000.00000, 1,821,155,000.000000, 1,825,852,000.000000, 1,826,706,000.000000, 1,911,252,000.000000]

I tried to achieve it using multiple ways but every time I get error like:

could not convert string to float

I understand that the number is huge, so I also tried converting into to numpy array and then use

y = x.astype(np.float)

y = np.asarray(x, dtype=np.float64)

I even tried reading the csv file and enforcing the datatype of respective columns as float but didn't work either.

Thanks

  • 1
    Please see https://stackoverflow.com/questions/6633523/how-can-i-convert-a-string-with-dot-and-comma-into-a-float-in-python/6633912#6633912 . The built-in float conversions do not allow for `,` in the text. – Karl Knechtel Nov 17 '20 at 00:46
  • 1
    Hi, perhaps this might be of interest to handle the thousands separator https://stackoverflow.com/questions/22137723/convert-number-strings-with-commas-in-pandas-dataframe-to-float – IronMan Nov 17 '20 at 00:46
  • what you need is that: `x1 = map(lambda item: float(item.replace(',', '')), x)` and then use `x1` – Doz Parp Nov 17 '20 at 00:47
  • 1
    It is best to copy and paste the actual error message, rather than saying "error like". – Mark Ransom Nov 17 '20 at 00:51
  • there are thousands of number in the list and error message reads "could not covert the string to float [some number from the list]" which appears some random number from the list. – Saqib Ali khan Nov 17 '20 at 01:16
  • `numpy` cannot handle the thousands separator directly. I'd try to do the conversion in `pandas` first. I believe it has more facilities in that style. – hpaulj Nov 17 '20 at 01:40

1 Answers1

2

try this:

x = ["1,784,006,000.00000", "1,821,155,000.000000", "1,825,852,000.000000", "1,826,706,000.000000",  "1,911,252,000.000000"]

def convert(s):
    return float(''.join(s.split(',')))

np.array(list(map(convert, x)))
Julien
  • 13,986
  • 5
  • 29
  • 53