【发布时间】:2020-09-12 03:19:34
【问题描述】:
我想为我的 pandas 数据框中尽可能多的列绘制直方图。
数据框中的所有数据都以字符串格式开头。我尝试在将数据类型输入hist() 之前对其进行转换,如果无法转换给定列,则会引发异常。
我希望在输出中看到一些直方图。我只会在无法生成绘图时收到错误消息This column can not be represented as a histogram 和<Figure size 432x288 with 0 Axes>。
感谢您的帮助!
# PACKAGES
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# DATA
data = {'col1': ['id345', 'id873', 'id972', 'id472', 'id930'],
'col2': ['1.0', '0.0', '1.0', '0.0', np.nan],
'col3': ['0.281', '0.380', '0.240', '0.260', '0.222'],
'col4': ['0.17', '0.184', '0', '0.22', np.nan],
'col5': ['1', '1', '0', np.nan, '0']
}
df = pd.DataFrame(data, columns = ['col1', 'col2', 'col3', 'col4', 'col5'])
# PLOTS
for col in df:
try:
plt.figure()
df.hist([int(col)])
except ValueError:
print('This column can not be represented as a histogram')
break
【问题讨论】:
标签: python matplotlib exception histogram valueerror