KApril
KApril

Reputation: 690

How to print variable dtype only object in Python

I am very new to use Python. I would like to print the variables that are 'object' dtype. Can you please help me with the syntax. I tried the below code

df.info()

but it gives the list of all variables with the dtypes float and int. I just want to print object dtype variables only.

For example: When I used df.info() I got the below results. but I dont want the other dtypes except object.

RangeIndex: 1460 entries, 0 to 1459
Data columns (total 81 columns):
Id               1460 non-null int64
MS               1460 non-null int64
MSZ              1460 non-null object
Lo               1201 non-null float64
LA               1460 non-null int64
St               1460 non-null object
Al               91 non-null object ```

Upvotes: 1

Views: 2145

Answers (1)

Stef
Stef

Reputation: 30589

I want to print the variables from the dataframe that are only object dtype

df = pd.DataFrame({'Id': [1], 'Name': ['xyz'], 'Weight': [12.34], 'Date': pd.to_datetime('2020-01-01'), '?': None})

df.select_dtypes('O').columns.to_list()
#['Name', '?']

Or, if you want to print the names one per line:

print(*df.select_dtypes('O').columns.to_list(), sep='\n')
#Name
#?

Upvotes: 1

Related Questions