How to get pandas.DataFrame columns containing specific dtype

Charlie_M

I'm using df.columns.values to make a list of column names which I then iterate over and make charts, etc... but when I set this up I overlooked the non-numeric columns in the df. Now, I'd much rather not simply drop those columns from the df (or a copy of it). Instead, I would like to find a slick way to eliminate them from the list of column names.

Now I have:

names = df.columns.values 

what I'd like to get to is something that behaves like:

names = df.columns.values(column_type=float64) 

Is there any slick way to do this? I suppose I could make a copy of the df, and drop those non-numeric columns before doing columns.values, but that strikes me as clunky.

Welcome any inputs/suggestions. Thanks.

Woody Pride

Someone will give you a better answe than this possibly, but one thing I tend to do is if all my numeric data are int64 or float64 objects, then you can create a dict of the column data types and then use the values to create your list of columns.

So for example, in a dataframe where I have columns of type float64, int64 and object firstly you can look at the data types as so:

DF.dtypes

and if they conform to the standard whereby the non-numeric columns of data are all object types (as they are in my dataframes), then you can do the following to get a list of the numeric columns:

[key for key in dict(DF.dtypes) if dict(DF.dtypes)[key] in ['float64', 'int64']]

Its just a simple list comprehension. Nothing fancy. Again, though whether this works for you will depend upon how you set up you dataframe...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Changing the dtype for specific columns in a pandas dataframe

From Dev

How to get row index of pandas dataframe containing specific values from two columns?

From Java

pandas how to check dtype for all columns in a dataframe?

From Dev

pandas how to check dtype for all columns in a dataframe?

From Dev

How to identify DataFrame columns containing specific pattern

From Dev

Pandas dataframe: how to summarize columns containing value

From Dev

Pandas how to reshape a dataframe containing duplicated values for columns

From Dev

How to drop DataFrame columns based on dtype

From Dev

How to get rows that has certain columns containing same values in pandas?

From Dev

Select specific columns in pandas DataFrame

From Dev

How can I "unpivot" specific columns from a pandas DataFrame?

From Dev

Why does apply change dtype in pandas dataframe columns

From Dev

Selecting Pandas Columns by dtype

From Dev

select columns based on columns names containing a specific string in pandas

From Dev

Convert Pandas dtype of dataframe

From Dev

How to get value counts for multiple columns at once in Pandas DataFrame?

From Dev

Pandas DataFrame: How to natively get minimum across range of rows and columns

From Dev

How to get percentage count based on multiple columns in pandas dataframe?

From Dev

How to get selective columns and then rows from a a pandas dataframe

From Dev

How do I create a function that will accept a pandas dataframe and remove rows containing a specific value?

From Dev

forward fill specific columns in pandas dataframe

From Dev

Pandas DataFrame select the specific columns with NaN values

From Dev

Add values and columns in specific order to Pandas DataFrame

From Dev

Slicing a pandas dataframe to the first instance of all columns containing a value

From Dev

Adding column to pandas DataFrame containing list of other columns' values

From Dev

Iterate over pandas dataframe columns containing nested arrays

From Dev

Slicing a pandas dataframe to the first instance of all columns containing a value

From Dev

How to concat Pandas dataframe columns

From Dev

How to reduce columns in dataframe pandas

Related Related

  1. 1

    Changing the dtype for specific columns in a pandas dataframe

  2. 2

    How to get row index of pandas dataframe containing specific values from two columns?

  3. 3

    pandas how to check dtype for all columns in a dataframe?

  4. 4

    pandas how to check dtype for all columns in a dataframe?

  5. 5

    How to identify DataFrame columns containing specific pattern

  6. 6

    Pandas dataframe: how to summarize columns containing value

  7. 7

    Pandas how to reshape a dataframe containing duplicated values for columns

  8. 8

    How to drop DataFrame columns based on dtype

  9. 9

    How to get rows that has certain columns containing same values in pandas?

  10. 10

    Select specific columns in pandas DataFrame

  11. 11

    How can I "unpivot" specific columns from a pandas DataFrame?

  12. 12

    Why does apply change dtype in pandas dataframe columns

  13. 13

    Selecting Pandas Columns by dtype

  14. 14

    select columns based on columns names containing a specific string in pandas

  15. 15

    Convert Pandas dtype of dataframe

  16. 16

    How to get value counts for multiple columns at once in Pandas DataFrame?

  17. 17

    Pandas DataFrame: How to natively get minimum across range of rows and columns

  18. 18

    How to get percentage count based on multiple columns in pandas dataframe?

  19. 19

    How to get selective columns and then rows from a a pandas dataframe

  20. 20

    How do I create a function that will accept a pandas dataframe and remove rows containing a specific value?

  21. 21

    forward fill specific columns in pandas dataframe

  22. 22

    Pandas DataFrame select the specific columns with NaN values

  23. 23

    Add values and columns in specific order to Pandas DataFrame

  24. 24

    Slicing a pandas dataframe to the first instance of all columns containing a value

  25. 25

    Adding column to pandas DataFrame containing list of other columns' values

  26. 26

    Iterate over pandas dataframe columns containing nested arrays

  27. 27

    Slicing a pandas dataframe to the first instance of all columns containing a value

  28. 28

    How to concat Pandas dataframe columns

  29. 29

    How to reduce columns in dataframe pandas

HotTag

Archive