Mailing List Archive

Using astype(int) for strings with thousand separator
Hi

While reading a csv file, some cells have values like '1,024' which I mean they contains thousand separator ','. Therefore, when I want to process them with

  row = df.iloc[0].astype(int)

I get the following error

  ValueError: invalid literal for int() with base 10: '1,024'


How can I fix that? Any idea?



Regards,
Mahmood
--
https://mail.python.org/mailman/listinfo/python-list
Re: Using astype(int) for strings with thousand separator [ In reply to ]
> On 14 Nov 2021, at 15:41, Mahmood Naderan via Python-list <python-list@python.org> wrote:
>
> Hi
>
> While reading a csv file, some cells have values like '1,024' which I mean they contains thousand separator ','. Therefore, when I want to process them with
>
> row = df.iloc[0].astype(int)

remove the "," from the sting seems the simplest things to do.

Use string's replace() to remove the comma.

Barry

>
> I get the following error
>
> ValueError: invalid literal for int() with base 10: '1,024'
>
>
> How can I fix that? Any idea?
>
>
>
> Regards,
> Mahmood
> --
> https://mail.python.org/mailman/listinfo/python-list

--
https://mail.python.org/mailman/listinfo/python-list
Re: Using astype(int) for strings with thousand separator [ In reply to ]
On 14.11.21 16:41, Mahmood Naderan via Python-list wrote:
> Hi
>
> While reading a csv file, some cells have values like '1,024' which I mean they contains thousand separator ','. Therefore, when I want to process them with
>
>   row = df.iloc[0].astype(int)


If you are reading a CSV with pandas.read_csv, that function takes a
parameter 'thousands' that should do what you're looking for (see
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html)

>
> I get the following error
>
>   ValueError: invalid literal for int() with base 10: '1,024'
>
>
> How can I fix that? Any idea?
>


--
https://mail.python.org/mailman/listinfo/python-list
Re: Using astype(int) for strings with thousand separator [ In reply to ]