I try to use a loop to do some operations on the Pandas numeric and category columns.
df = sns.load_dataset('diamonds')
print(df.dtypes,'\n')
carat float64
cut category
color category
clarity category
depth float64
table float64
price int64
x float64
y float64
z float64
dtype: object
In the following codes, I just simply cut and paste 'float64' and 'category' from the preceding step output.
for i in df.columns:
if df[i].dtypes in ['float64']:
print(i)
for i in df.columns:
if df[i].dtypes in ['category']:
print(i)
I found that it works for 'float64' but generates an error for 'category'.
Why is this ? Thanks very much !!!
carat
depth
table
x
y
z
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-74-8e6aa9d4726e> in <module>
4
5 for i in df.columns:
----> 6 if df[i].dtypes in ['category']:
7 print(i)
TypeError: data type 'category' not understood