Python Forum
Convert several columns to int in dataframe
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert several columns to int in dataframe
#1
I have imported data into dataframe. Some columns have been formatted as float64 but when I try to convert to int, I get a ValueError and 'cannot convert non-finite values to integer'??
Here is my code. Can anyone advise where I have gone wrong?
df_16[['1.1 Large employers and higher managerial and administrative occupations', '1.2 Higher professional occupations', '2. Lower managerial, administrative and professional occupations', '3. Intermediate occupations', '4. Small employers and own account workers', '5. Lower supervisory and technical occupations', '6. Semi-routine occupations', '7. Routine occupations', 'L14.1 Never worked', 'L14.2 Long-term unemployed', 'L15 Full-time students', 'L17 Not classifiable for other reasons']] = df_16[['1.1 Large employers and higher managerial and administrative occupations', '1.2 Higher professional occupations', '2. Lower managerial, administrative and professional occupations', '3. Intermediate occupations', '4. Small employers and own account workers', '5. Lower supervisory and technical occupations', '6. Semi-routine occupations', '7. Routine occupations', 'L14.1 Never worked', 'L14.2 Long-term unemployed', 'L15 Full-time students', 'L17 Not classifiable for other reasons']].round(0).astype(int)
Reply
#2
please format your code You can't show it that way ;

probably missing values in your data, chek that: df.isnull().sum()
Krayna likes this post
Reply
#3
Yes that was the issue. Thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Outputs "NaN" after "DataFrame columns" function? epsilon 7 1,270 Jan-27-2021, 10:59 AM
Last Post: epsilon
  Adapting a dataframe to the some of columns flyway 2 956 Aug-12-2020, 07:21 AM
Last Post: flyway
  Difference of two columns in Pandas dataframe zinho 2 1,619 Jun-17-2020, 03:36 PM
Last Post: zinho
  DataFrame: To print a column value which is not null out of 5 columns mani 2 1,070 Mar-18-2020, 06:07 AM
Last Post: mani
  Convert dataframe string column to numeric in Python darpInd 1 1,232 Mar-14-2020, 10:07 AM
Last Post: ndc85430
Question Dividing a single column of dataframe into multiple columns based on char length darpInd 2 1,065 Mar-14-2020, 09:19 AM
Last Post: scidam
  Interate for loop over certain columns in dataframe Finpyth 2 1,022 Mar-06-2020, 08:34 AM
Last Post: Finpyth
  How to highlight dataframe columns SriRajesh 1 1,054 Feb-28-2020, 11:02 PM
Last Post: Marbelous
  Does the order of columns in the DataFrame matter? new_to_python 11 2,586 Feb-17-2020, 12:32 AM
Last Post: scidam
  Pandas dataframe columns collapsed in Spyder when printing UniKlixX 2 2,626 Nov-04-2019, 07:00 AM
Last Post: UniKlixX

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020