- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个 DataFrame(多个每日时间序列),DateTimeIndex
作为 index
,MultiIndex
作为 columns
。我想选择一列并创建一个箱形图,其中数据按年份分组。我认为这很容易,但我正在努力获得一些结果。
>>> daily.shape
(11319, 118)
>>> daily.index
DatetimeIndex(['1986-01-01', '1986-01-02', '1986-01-03', '1986-01-04',
'1986-01-05', '1986-01-06', '1986-01-07', '1986-01-08',
'1986-01-09', '1986-01-10',
...
'2016-12-22', '2016-12-23', '2016-12-24', '2016-12-25',
'2016-12-26', '2016-12-27', '2016-12-28', '2016-12-29',
'2016-12-30', '2016-12-31'],
dtype='datetime64[ns]', name='timevalue', length=11319, freq=None)
>>> daily.columns
MultiIndex(levels=[['41B001', '41B004', '41B006', '41B008', '41B011', '41MEU1', '41N043', '41R001', '41R002', '41R012', '41WOL1', '41WOL2', '47E013', 'T1M001', 'T1M003'], ['BA-10.0', 'BA-2.5', 'BC', 'CO', 'CO2', 'NO', 'NO2', 'NOx', 'O3', 'PM-10.0', 'PM-2.5', 'RH', 'SO2', 'T', 'UVPM', 'VO-10.0', 'VO-2.5', 'WD', 'WS-s', 'WS-v', 'p']],
labels=[[0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14], [5, 6, 7, 3, 5, 6, 7, 8, 3, 5, 6, 7, 8, 3, 5, 6, 7, 12, 0, 1, 5, 6, 7, 8, 9, 10, 15, 16, 0, 1, 5, 6, 7, 9, 10, 15, 16, 0, 1, 2, 3, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 0, 1, 2, 3, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 2, 3, 4, 5, 6, 7, 12, 14, 0, 1, 2, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 0, 2, 3, 4, 5, 6, 7, 8, 9, 12, 14, 15, 4, 5, 6, 7, 12, 11, 13, 13, 17, 18, 19, 20, 11, 13, 13, 17, 18, 19, 20]],
names=['sitekey', 'measurandkey'])
我能做到的最好的是:
fig, axe = plt.subplots()
daily.loc[:,[('41R001', 'SO2')]].groupby(daily.index.map(lambda x: x.year)).boxplot(ax=axe, subplots=False, rot=90)
但它需要其他后处理来标记轴。
当我尝试 reset_index()
应用函数并使用 pivot()
时,由于 MultiIndex
,我出现了索引错误。
d = daily.reset_index()
d['timevalue']
异常(exception)是:无法处理非唯一的多索引!我不明白,因为我的 MultiIndex 中没有出现 TimeValue。我也尝试过 .loc[]
但我认为问题出在其他地方。
所以,我要实现的目标很简单:
loc
和上面示例中的复合键)并获得一个时间序列箱线图,其中数据按年份分组。我认为这很容易,但由于多索引错误,我无法将 pivot()
正确地用于此 DataFrame。
最佳答案
如果你不介意使用 seaborn
库,你可以很容易地制作这个图:
import pandas as pd
import seaborn as sns
index = pd.DatetimeIndex(start=pd.to_datetime('1985-01-01'),
end = pd.to_datetime('2017-03-08'),
freq='d')
df = pd.DataFrame(index = index,
data = np.random.uniform(-1,1,size=(index.shape[0],4)),
columns=pd.MultiIndex.from_arrays([['A','A','B','B'],
['d','e','d','e']]))
df['Year'] = df.index.year
# A B Year
# d e d e
# 1985-01-01 0.205208 -0.228484 0.296273 0.545031 1985
# 1985-01-02 0.546436 -0.538920 0.173388 0.848590 1985
# 1985-01-03 -0.367593 -0.974911 -0.796331 -0.946239 1985
# 1985-01-04 -0.346102 -0.951542 -0.975172 0.951099 1985
# 1985-01-05 0.973975 0.708254 -0.150454 0.145298 1985
ax = sns.boxplot(data = df, x='Year',y=('A','e'))
for item in ax.get_xticklabels():
item.set_rotation(90)
生成的图像:
我尝试使用 pandas.DataFrame.boxplot()
方法,但无法在短时间内为这种情况工作=)。
关于python - Pandas 的年度箱线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43065157/
在表单数据提交到 MySQL 数据库之前,我使用 PHP 来验证表单输入。 仅验证年度表单输入的最佳方法是什么?我有一个表单输入,用户应该在其中输入一个日期,仅以一年的形式输入。显然我可以检查输入的数
是否可以创建一个 sql 语句来动态生成 3 行而无需任何表..这些将包括 value1 : 10 | value2: 10 value2 : 11 | value2: 11 value3 : 12
我有以下 SQL 数据库:表名 date fname surname points display 2015-08-08 John Lennon 5
我们的 iOS 应用程序仅通过 https 进行调用,而没有其他任何方式,因此我们必须提交我们的 self 分类报告。我们填写了每个字段,但我们不知道 ECCN 是 5D002 还是 5D992 还是
我们将从以下数据表开始: id date 1: 1 2015-12-31 2: 1 2014-12-31 3: 1 2013-12-31 4: 1 2012-12-
已关闭。此问题旨在寻求有关书籍、工具、软件库等的建议。不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以
我在 PHP 的文档中看到有两种方法可以将年份值格式化为 4 位数字: Y - A full numeric representation of a year, 4 digits o - ISO-86
我是一名优秀的程序员,十分优秀!