python从数据框列绘制直方图 [英] python plotting a histogram from dataframe column

查看:74
本文介绍了python从数据框列绘制直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了绘制从数据帧发出的直方图,我似乎缺乏转换为matplotlib可以处理的正确对象类型的能力.以下是一些失败的尝试.我该如何解决?

更一般而言,您通常如何挽救类似的东西?

将 numpy 导入为 np将熊猫作为pd导入从 matplotlib 导入 pyplot 作为 pltfilter(lambda v:v> 0,df ['foo_col']).hist(bins = 10)

<块引用>

---> 10过滤器(lambda v:v> 0,df ['foo_col']).hist(bins = 100)AttributeError: 'filter' 对象没有属性 'hist'

hist(filter(lambda v: v > 0, df['foo_col']), bins=100)

<块引用>

---> 10 hist(filter(lambda v: v > 0, df['foo_col']), bins=100)TypeError:系列"对象不可调用

解决方案

通过所有帐户, 解决方案

By all accounts, filter is lucky to be part of the standard library. IIUC, you just want to filter your dataframe to plot a histogram of values > 0. Pandas has its own syntax for that:

import numpy as np
import pandas as pd

import matplotlib.pyplot as plt

data = np.random.randint(-50, 1000, 10000)

df = pd.DataFrame({'some_data': data})

df[df['some_data'] >= 0].hist(bins=100)
plt.show()

Note that this will run much faster than python builtins could ever hope to (it doesn't make much difference in my trivial example, but it will with bigger datasets). It's important to use pandas methods with dataframes wherever possible because, in many cases, the calculation will be vectorized and run in highly optimised C/C++ code.

这篇关于python从数据框列绘制直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆