How do I make a loop where I plot figures based on dtypes?

Viewed 26

I require help with a for loop I was creating to plot different figures based on if a variable were numeric or categorical.

So far, the issue I have is the if statement not working. I am not sure why, it looks like a simple thing: if the dtypes is numeric, do a boxplot, else do another thing. Why is this simple thing not being solved?

f, axes = plt.subplots(4,2, figsize = (40,40))
count1 = 0
count2 = 2
for var in numdata:
    temp = pd.DataFrame(data[var])
    if temp.dtypes == 'int64':
        sb.boxplot(x = var, data = numdata, orient = 'h', color = 'r', ax = axes[count1, 0])
        sb.histplot(x = var, data = numdata, color = 'g', ax = axes[count1, 1])
        count1 += 1
    else:
        sb.catplot(y = var, data = numdata, kind = 'count', ax = axes[count2,0])
        sb.boxplot(x = var, y = 'SalePrice', data = numdata, ax = axes[count2,1])
        count2 += 1
1 Answers

I'll suppose that data is a dictionary of numpy arrays, in that case your problem could be that you don't only have int64, thus you could change your if statement to something like:

for var in numdata:
    temp = data[var][0]
    if isinstance(temp, float) or isinstance (temp, int):
         .
         .
         . 
Related