I would like to find out which columns of a dataframe are categorical. This dataframe has indeed column z but my code cannot detect it and prints an empty list. How should I fix it?
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
data=[[ 10,10,'a'],
[ 15,15,'a'],
[ 14,14,'b']
,[16,16,'b'],
[19,19,'a'],
[17,17,'a']
,[6,6,'c'],
[5,5,'b'],
[20,20,'c']
,[22,22,'c'],
[21,21,'b'],
[18,45 ,'a']]
df = pd.DataFrame(data, columns=['x','y','z'])
categorical_values=[]
for i in df.columns.values.tolist():
if (type(df[i].all()))==str:
categorical_values.append(i)
print(categorical_values, 'CATEGORICAL VALUES')
print(len(categorical_values),'total of categorical variables')