Plotting Histogram for all columns in a Data Frame

As an alternative to the for loop approach, I think you can try this:

df.hist(bins=30, figsize=(15, 10))

This will plot a histogram for each numerical attribute in the df DataFrame. Here, the bins and figsize arguments are just for customizing the output.


The problem is that your for loop:

for x in range(0, len(df.columns)):

Will iterate over a range of integers. Then when you try to access the column via:

df.select(x)

You will get an error because x is not a valid column identifier.

Instead, change your loop to:

for x in df.columns:

and the rest of your code will work.