【问题标题】:How to plot the daily maximum values如何绘制每日最大值
【发布时间】:2021-09-01 03:55:24
【问题描述】:

我正在尝试绘制数据框列 (ext_temp) 每天的最大值:

import pandas as pd

data = {'vin': {0: 'VF1AG0000KF908155', 1: 'VF1AG0000KF908155', 2: 'VF1AG0000KF908155', 3: 'VF1AG0000KF908155', 4: 'VF1AG0000KF908155', 5: 'VF1AG0000KF908155', 6: 'VF1AG0000KF908155', 7: 'VF1AG0000KF908155', 8: 'VF1AG0000KF908155', 9: 'VF1AG0000KF908155'}, 'date': {0: pd.Timestamp('2019-09-27 07:07:02'), 1: pd.Timestamp('2019-09-27 09:23:08'), 2: pd.Timestamp('2019-09-27 09:39:08'), 3: pd.Timestamp('2020-07-15 11:46:41'), 4: pd.Timestamp('2020-07-16 07:17:52'), 5: pd.Timestamp('2020-07-16 09:23:47'), 6: pd.Timestamp('2020-09-11 07:43:05'), 7: pd.Timestamp('2020-09-17 15:00:33'), 8: pd.Timestamp('2020-10-21 06:49:58'), 9: pd.Timestamp('2020-10-21 14:47:33')}, 'sohe': {0: 101, 1: 101, 2: 101, 3: 96, 4: 96, 5: 96, 6: 96, 7: 96, 8: 96, 9: 96}, 'soc': {0: 60, 1: 63, 2: 99, 3: 66, 4: 68, 5: 69, 6: 86, 7: 58, 8: 9, 9: 9}, 'ext_temp': {0: 27, 1: 30, 2: 31, 3: 30, 4: 26, 5: 29, 6: 26, 7: 29, 8: 28, 9: 27}, 'battery_temp': {0: 27, 1: 33, 2: 32, 3: 26, 4: 26, 5: 26, 6: 26, 7: 30, 8: 27, 9: 29}}
df = pd.DataFrame(data)

不幸的是,在尝试使用时

nd = "VF1AG0000KF908155"
df = charge[charge.vin==gop]

df = df.groupby(pd.Grouper(key = 'date', freq = 'D'))

fig,ax = plt.subplots()
ax.plot(df.date, df['ext_temp'].max())

我收到以下错误消息: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray

【问题讨论】:

    标签: python pandas dataframe matplotlib bar-chart


    【解决方案1】:
    • 使用pd.Grouper has 将用NaN 填充缺失的日期
    • 如果您不想填写缺失的日期,请使用.dt 提取器按'date' 的日期组件分组。
    • 使用pandas.DataFrame.plot 绘制数据框
      • 使用了kind='bar',因为没有太多数据。对于线图,请使用kind='line'

    pd.Grouper

    • 注意需要使用.dropna(),至少要绘制条形图。
    dfg = df.groupby(pd.Grouper(key='date', freq='D'))['ext_temp'].max().dropna()
    
    ax = dfg.plot(kind='bar')
    

    dfg = df.groupby(pd.Grouper(key='date', freq='D'))['ext_temp'].max().dropna()
    
    ax = dfg.plot(kind='line')
    

    .dt.date

    • 仅对 'date' 列的 date 组件进行分组
    dfg = df.groupby(df.date.dt.date)['ext_temp'].max()
    
    ax = dfg.plot(kind='bar')
    

    【讨论】:

      猜你喜欢
      • 2014-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-19
      • 1970-01-01
      • 2017-06-22
      • 1970-01-01
      相关资源
      最近更新 更多