Python Forum
Changing Column dtypes in DataFrame
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Changing Column dtypes in DataFrame
#1
Hi everyone,

I have a DF with roughly 700 columns. I'd like to create a loop which loops through columns updating dtype category columns to object columns.I have created this loop but is giving me an error. Can anyone help?

for col in df.columns:
    col_type = df[col].dtype
    
    if col_type = category:
        df[col] = df[col].astype('object')
Thanks
Reply


Messages In This Thread
Changing Column dtypes in DataFrame - by Scott - Jan-22-2020, 01:10 AM
RE: Changing Column dtypes in DataFrame - by Scott - Jan-22-2020, 02:33 AM
RE: Changing Column dtypes in DataFrame - by Scott - Jan-22-2020, 10:36 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Find duplicates in a pandas dataframe list column on other rows Calab 2 2,586 Sep-18-2024, 07:38 PM
Last Post: Calab
  Find strings by index from a list of indexes in a different Pandas dataframe column Calab 3 1,855 Aug-26-2024, 04:52 PM
Last Post: Calab
  Create new column in dataframe Scott 10 4,079 Jun-30-2024, 10:18 PM
Last Post: Scott
  attempt to split values from within a dataframe column mbrown009 9 6,434 Jun-20-2024, 07:59 PM
Last Post: AdamHensley
  Putting column name to dataframe, can't work. jonah88888 2 3,433 Jun-18-2024, 09:19 PM
Last Post: AdamHensley
  concat 3 columns of dataframe to one column flash77 2 2,292 Oct-03-2023, 09:29 PM
Last Post: flash77
  HTML Decoder pandas dataframe column mbrown009 3 2,913 Sep-29-2023, 05:56 PM
Last Post: deanhystad
  New Dataframe Column Based on Several Conditions nb1214 1 2,648 Nov-16-2021, 10:52 PM
Last Post: jefsummers
  Setting the x-axis to a specific column in a dataframe devansing 0 2,722 May-23-2021, 12:11 AM
Last Post: devansing
Question [Solved] How to refer to dataframe column name based on a list lorensa74 1 3,142 May-17-2021, 07:02 AM
Last Post: lorensa74

Forum Jump:

User Panel Messages

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