在数据帧列上循环以绘制直方图


%matplotlib inline
for column in df.columns:
if df[column].dtype =="int64":
df[column].hist(title=column)
else:
df[column].plot(kind="bar", title=column)
AttributeError: 'Rectangle' object has no property 'title'

如果dtype是int,我想打印Histogram,如果dttype是object,则打印barplot,但代码不起作用。

尝试在开始时对列进行切片,您需要子图/子图来绘制多个图

import seaborn as sns
numeric_columns = df.select_dtypes(include=['int64','float64']).columns
n_rows = 2
n_cols= 2
for i, column in enumerate(df.columns,1):
plt.subplot(n_rows,n_cols,i)
if column in numeric_columns:
df[column].plot(kind="hist", title=column)
else:
sns.countplot(df[column])

正如Code Different所提到的,如果你使用的是一个旧的Panda版本,你想得到标题争论

最新更新