【问题标题】:Why isn't this code to plot a histogram on a continuous value Pandas column working?为什么这段代码不能在连续值 Pandas 列上绘制直方图?
【发布时间】:2017-07-18 16:49:59
【问题描述】:

我正在尝试在 140 万行大熊猫数据框中的连续值列 Trip_distance 上创建直方图。写了如下代码:

fig = plt.figure(figsize=(17,10))
trip_data.hist(column="Trip_distance")
plt.xlabel("Trip_distance",fontsize=15)
plt.ylabel("Frequency",fontsize=15)
plt.xlim([0.0,100.0])
#plt.legend(loc='center left', bbox_to_anchor=(1.0, 0.5))

但我不确定为什么所有值都给出相同的频率图,但事实并非如此。代码有什么问题?

测试数据:

    VendorID    lpep_pickup_datetime    Lpep_dropoff_datetime   Store_and_fwd_flag  RateCodeID  Pickup_longitude    Pickup_latitude Dropoff_longitude   Dropoff_latitude    Passenger_count Trip_distance   Fare_amount Extra   MTA_tax Tip_amount  Tolls_amount    Ehail_fee   improvement_surcharge   Total_amount    Payment_type    Trip_type
0   2   2015-09-01 00:02:34 2015-09-01 00:02:38 N   5   -73.979485  40.684956   -73.979431  40.685020   1   0.00    7.8 0.0 0.0 1.95    0.0 NaN 0.0 9.75    1   2.0
1   2   2015-09-01 00:04:20 2015-09-01 00:04:24 N   5   -74.010796  40.912216   -74.010780  40.912212   1   0.00    45.0    0.0 0.0 0.00    0.0 NaN 0.0 45.00   1   2.0
2   2   2015-09-01 00:01:50 2015-09-01 00:04:24 N   1   -73.921410  40.766708   -73.914413  40.764687   1   0.59    4.0 0.5 0.5 0.50    0.0 NaN 0.3 5.80    1   1.0
3   2   2015-09-01 00:02:36 2015-09-01 00:06:42 N   1   -73.921387  40.766678   -73.931427  40.771584   1   0.74    5.0 0.5 0.5 0.00    0.0 NaN 0.3 6.30    2   1.0
4   2   2015-09-01 00:00:14 2015-09-01 00:04:20 N   1   -73.955482  40.714046   -73.944412  40.714729   1   0.61    5.0 0.5 0.5 0.00    0.0 NaN 0.3 6.30    2   1.0
5   2   2015-09-01 00:00:39 2015-09-01 00:05:20 N   1   -73.945297  40.808186   -73.937668  40.821198   1   1.07    5.5 0.5 0.5 1.36    0.0 NaN 0.3 8.16    1   1.0
6   2   2015-09-01 00:00:52 2015-09-01 00:05:50 N   1   -73.890877  40.746426   -73.876923  40.756306   1   1.43    6.5 0.5 0.5 0.00    0.0 NaN 0.3 7.80    1   1.0
7   2   2015-09-01 00:02:15 2015-09-01 00:05:34 N   1   -73.946701  40.797321   -73.937645  40.804516   1   0.90    5.0 0.5 0.5 0.00    0.0 NaN 0.3 6.30    2   1.0
8   2   2015-09-01 00:02:36 2015-09-01 00:07:20 N   1   -73.963150  40.693829   -73.956787  40.680531   1   1.33    6.0 0.5 0.5 1.46    0.0 NaN 0.3 8.76    1   1.0
9   2   2015-09-01 00:02:13 2015-09-01 00:07:23 N   1   -73.896820  40.746128   -73.888626  40.752724   1   0.84    5.5 0.5 0.5 0.00    0.0 NaN 0.3 6.80    2   1.0
In [ ]:

Trip_distance column 

0     0.00
1     0.00
2     0.59
3     0.74
4     0.61
5     1.07
6     1.43
7     0.90
8     1.33
9     0.84
10    0.80
11    0.70
12    1.01
13    0.39
14    0.56
Name: Trip_distance, dtype: float64

100 个 bin 之后:

【问题讨论】:

  • 您能否包含一个给出意外结果的测试数据集,并描述您希望看到的结果?
  • 已添加。如果您看到我的 Trip_distance 列的值有不同的值,但我得到的图表仍然具有相同的频率计数?
  • 行程距离的取值范围是多少?日期时间列很难复制。
  • 还有多少数据可以让您获得 140000 的频率?我怀疑可能只是数据过多。
  • 总行数为 1.4MM。我更新了 trip_distance 样本值

标签: python pandas histogram


【解决方案1】:

编辑:

在您的 cmets 之后,这实际上完全可以理解为什么您没有得到每个不同值的直方图。有 140 万行和十个离散桶。所以显然每个桶正好是 10%(在你可以在图中看到的范围内)。


快速重新运行您的数据:

In [25]: df.hist(column='Trip_distance')

打印效果很好。

df.hist 函数带有一个可选的关键字参数 bins=10,它将数据分桶到离散的 bin 中。只有 10 个离散的 bin 和数十万行的或多或少均匀分布,您可能无法在低分辨率图中看到 10 个不同 bin 的差异:

In [34]: df.hist(column='Trip_distance', bins=50)

【讨论】:

  • 我刚刚在 100 个垃圾箱后用图表更新了我的问题。虽然看起来不太好
  • 我的意思是,绝对放弃 xlim=[0,100] 你没有超过 60 次的行程
  • 我刚到 40 岁。
  • 我有一个问题,你怎么能保存这个数字?
【解决方案2】:

这是绘制数据的另一种方法,涉及将 date_time 转换为索引,这可能会帮助您将来进行切片

#convert column to datetime
trip_data['lpep_pickup_datetime'] = pd.to_datetime(trip_data['lpep_pickup_datetime'])
#turn the datetime to an index
trip_data.index = trip_data['lpep_pickup_datetime']
#Plot
trip_data['Trip_distance'].plot(kind='hist')
plt.show()

【讨论】:

  • 可以指定 bins (kind='hist', bins=20)
猜你喜欢
  • 2018-10-18
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
  • 2017-12-28
  • 1970-01-01
  • 1970-01-01
  • 2012-07-06
相关资源
最近更新 更多