0

I'm attempting to convert the following into integers. I have literally tried everything and keep getting errors.

For instance:

pop2007 = pop2007.astype('int32')

ValueError: invalid literal for int() with base 10: '4,779,736'

Below is the DF I'm trying to convert. I've even attempted the .values method with no success.

pop2007

Alabama            4,779,736
Alaska               710,231
Arizona            6,392,017
Arkansas           2,915,918
California        37,253,956
Colorado           5,029,196
Connecticut        3,574,097
Delaware             897,934
Florida           18,801,310
Georgia            9,687,653
Idaho              1,567,582
Illinois          12,830,632
Indiana            6,483,802
Iowa               3,046,355
Kansas             2,853,118
Kentucky           4,339,367
Louisiana          4,533,372
Maine              1,328,361
Maryland           5,773,552
Massachusetts      6,547,629
Michigan           9,883,640
Minnesota          5,303,925
Mississippi        2,967,297
Missouri           5,988,927
Montana              989,415
Nebraska           1,826,341
Nevada             2,700,551
New Hampshire      1,316,470
New Jersey         8,791,894
New Mexico           2059179
New York            19378102
North Carolina       9535483
North Dakota          672591
Ohio                11536504
Oklahoma             3751351
Oregon               3831074
Pennsylvania        12702379
Rhode Island         1052567
South Carolina       4625364
South Dakota          814180
Tennessee            6346105
Texas             25,145,561
Utah               2,763,885
Vermont              625,741
Virginia           8,001,024
Washington         6,724,540
West Virginia      1,852,994
Wisconsin          5,686,986
Wyoming              563,626
Name: 3, dtype: object
5
  • You can't convert an string with commas to integers Commented Apr 7, 2021 at 19:38
  • You can't have commas in the the strings. You can remove them with pop2007.replace(',', '') Commented Apr 7, 2021 at 19:38
  • @ThisIsAQuestion Thanks but I have tried that and it doesn't remove anything for some odd reason. I've also tried pop2007.values.replace(',', '') which returns: 'numpy.ndarray' object has no attribute 'replace' Commented Apr 7, 2021 at 19:45
  • In that case you'll have to iterate through your numbers and call .replace on each of them. Commented Apr 7, 2021 at 19:53
  • @ThisIsAQuestion how would I do that Commented Apr 7, 2021 at 20:03

2 Answers 2

2

You can't turn a string with commas into an integer. Try this.

my_int = '1,000,000'
my_int = int(my_int.replace(',', ''))
print(my_int)
Sign up to request clarification or add additional context in comments.

1 Comment

thanks but when I replace my_int with my ' pop2007 ' which is a string I get: TypeError: cannot convert the series to <class 'int'>
0

Have you tried pop2007.replace(',','') to remove the commas from your string values before converting to integers?

Comments

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.