Let I've a dataframe df
Name Age Job
Rick 24 Worker
Max 20 Worker
Sam 48 Driver
Expected output:
Name
Job
Now, I want to print out those column(name) which has object type data.
Here, Is my attempt:
for column in df:
if df.dtypes(column) == 'object':
print(column)
But I'm getting an error which is: " if df.dtypes(column) == 'object': TypeError: 'Series' object is not callable "
You can use df.select_dtypes as follows:
df.select_dtypes('object').columns.to_list()
Output:
['Name', 'Job']
Or:
for column in df:
if df[column].dtype == 'object':
print(column)
You can use list comprehension:
[x for x in df.columns if df[x].dtype == object]
Output:
['Name', 'Job']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With