How can I select columns in a Pandas DataFrame by datatype?

Viewed 492

I have a pandas dataframe of a standard shape:

   A     B     C       D        E................φ
1-Int   NaN Str Obj  Datetime  NaN..........Mixed Obj (like currency)
2-NaN Float Str Obj  Datetime Category..........NaN
3-Int Float   NaN    Datetime Category......Mixed Obj
.  .   .       .         .       .               .
.  .   .       .         .       .               .
.  .   .       .         .       .               .
Z-Int Float Str Obj     NaN   Category......Mixed Obj

In the example above, Z is an arbitrary row greater than 3. Φ is an arbitrary column name that represents a column greater than C. It could be the 90th column or the 150th column. My aim is to sift through the columns above replace values NaN values by datatype. My desired outcome is this:

   A     B     C       D        E......................φ
1-Int  0.00 Str Obj  Datetime Uncategorized.......Mixed Obj
2- 0  Float Str Obj  Datetime Category...............$0.00
3-Int Float  "None"  Datetime Category............Mixed Obj
.  .   .       .         .       .                     .
.  .   .       .         .       .                     .
.  .   .       .         .       .                     .
Z-Int Float Str Obj  0/00/0000 Category...........Mixed Obj

The goal is to have the ability to replace NaN values in specific columns which contain specific datatypes, with their datatype's version of 0. So 0 for integer, 0.00 for float, "None" for string, 0/00/0000 for Datetime (I know this may cause some problems), uncategorized for category, and $0.00 for mixed objects like currency.

To attempt this, I used pandas loc function to see if I could locate column by column is this were true.

for col in df.columns:
    print(df.loc[:,col].apply(isinstance, args = [int]))

The expected result was:

   A     
1-True   
2-False 
3-True  
.  .    
.  .  
.  .   
Z-True 

However I got:

   A     
1-False   
2-False 
3-False  
.  .    
.  .  
.  .   
Z-False

I don't understand why I couldn't identify the integers inside of this column.

1 Answers

You can use select_dtypes to get only the columns in a dataframe that match a specific type. For example, to get just the float columns you'd use:

df.select_dtypes(include='float64')

The include argument takes a string or list so you can specify multiple types if you want.

Related