Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在pandas.DataFrame.hist中按列删除_Python_Pandas - Fatal编程技术网

Python 在pandas.DataFrame.hist中按列删除

Python 在pandas.DataFrame.hist中按列删除,python,pandas,Python,Pandas,在指定按列a分组并将直方图限制为列f和g之后,我仍然有列a显示为绿色。有没有一种方法可以在不进入matplotlib或for循环的情况下删除它 axes = dfs.hist(column=['f', 'g'], by='a', layout=(1, 3), legend=True, bins=np.linspace(0, 8, 10), sharex=True, sharey=True) 这显然是熊猫图书馆的一个错误。当by是一个数字数据类型列时,问题似乎就出现了——

在指定按列
a
分组并将直方图限制为列
f
g
之后,我仍然有列
a
显示为绿色。有没有一种方法可以在不进入matplotlib或for循环的情况下删除它

axes = dfs.hist(column=['f', 'g'], by='a', layout=(1, 3), legend=True, bins=np.linspace(0, 8, 10),
            sharex=True, sharey=True)

这显然是熊猫图书馆的一个错误。当
by
是一个数字数据类型列时,问题似乎就出现了——它可能会将数据帧子集到
列和
by
中的标签,然后绘制该标签,当
by
是数字时,这是有问题的

您可以为定义
“by”
的列创建非数字标签,或者如果不想更改数据,只需在绘图前将类型重新指定给
对象即可

样本数据


提供样本数据肯定会有所帮助。。。看。@BigBen谢谢你的提醒。事实上,我应该包括示例数据。很高兴有Alolz将其包含在答案中。感谢您发现它。更改数据类型是一种简洁的方法。将在github上打开一个问题。
import pandas as pd
import numpy as np

df = pd.DataFrame({'length': np.random.normal(0, 1, 1000),
                   'width': np.random.normal(0, 1, 1000),
                   'a': np.random.randint(0, 2, 1000)})
# Problem with a numeric dtype for `by` column
df.hist(column=['length', 'width'], by='a', figsize=(4, 2))
# Works fine when column type is object
(df.assign(a=df['a'].astype('object'))
   .hist(column=['length', 'width'], by='a' , figsize=(4, 2)))