Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.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 如何在熊猫中创建groupby子地块?_Python_Pandas_Matplotlib_Seaborn - Fatal编程技术网

Python 如何在熊猫中创建groupby子地块?

Python 如何在熊猫中创建groupby子地块?,python,pandas,matplotlib,seaborn,Python,Pandas,Matplotlib,Seaborn,我有一个包含犯罪时间序列数据的数据框,其中包含犯罪方面的数据(如下所示)。我想在数据框上执行分组绘图,以便能够探索犯罪趋势 Offence Rolling year total number of offences Month 0 Criminal damage and arson 1001 2003-03-31 1 Drug offences

我有一个包含犯罪时间序列数据的数据框,其中包含犯罪方面的数据(如下所示)。我想在数据框上执行分组绘图,以便能够探索犯罪趋势

    Offence                     Rolling year total number of offences       Month
0   Criminal damage and arson   1001                                        2003-03-31
1   Drug offences               66                                         2003-03-31
2   All other theft offences    617                                   2003-03-31
3   Bicycle theft               92                                    2003-03-31
4   Domestic burglary           282                                   2003-03-31
我有一些代码可以完成这项工作,但它有点笨拙,而且它丢失了Pandas在单个绘图中提供的时间序列格式。(我附上了一张图片来说明)。有人能给我推荐一个这样的情节的成语吗

我会转向Seaborn,但我不知道如何将xlabel格式化为时间序列

[![subs = \[\]
for idx, (i, g) in enumerate(df.groupby("Offence")):
        subs.append({"data": g.set_index("Month").resample("QS-APR", how="sum" ).ix\["2010":\],
                     "title":i})

ax = plt.figure(figsize=(25,15))
for i,g in enumerate(subs):
    plt.subplot(5, 5, i)
    plt.plot(g\['data'\])
    plt.title(g\['title'\])
    plt.xlabel("Time")
    plt.ylabel("No. of crimes")
    plt.tight_layout()][1]][1]

这是连续6年从
pd.groupby()
获得的大熊猫6个散点图的可复制示例。在x轴上——是当年的石油价格(布伦特),在y轴上——是当年sp500的价值

import matplotlib.pyplot as plt
import pandas as pd
import Quandl as ql
%matplotlib inline

brent = ql.get('FRED/DCOILBRENTEU')
sp500 = ql.get('YAHOO/INDEX_GSPC')
values = pd.DataFrame({'brent':brent.VALUE, 'sp500':sp500.Close}).dropna()["2009":"2015"]

fig, axes = plt.subplots(2,3, figsize=(15,5))
for (year, group), ax in zip(values.groupby(values.index.year), axes.flatten()):
    group.plot(x='brent', y='sp500', kind='scatter', ax=ax, title=year)
这将生成以下绘图:

(以防万一,从这些曲线图中,你可以推断2010年石油和sp500之间有很强的相关性,但其他年份没有)

您可以在
group.plot()
中更改
kind
,使其适合您的特定种类或数据。我预计,如果您的数据中有x轴的日期格式,pandas将保留该格式。

在这种情况下非常有效

import matplotlib.pyplot as plt
import pandas as pd
import quandl as ql

df = ql.get(["NSE/OIL.1", "WIKI/AAPL.1"], start_date="2013-1-1")
df.columns = ['OIL', 'AAPL']
df['year'] = df.index.year

from altair import *
即#1-按年份无颜色/按年份无列

Viz#2-按年份无颜色/按年份列

Viz#3-按年份划分的颜色

您是否对连续6年的2x3石油与黄金价格散点图的可复制示例感兴趣,该散点图按年份分组
数值。分组依据(数值。指数。年份)
熊猫?当然。我只是想完成这个过程…我会尝试一下,让你知道。看起来比我的努力简单多了。
Chart(df).mark_point(size=1).encode(x='AAPL',y='OIL').configure_cell(width=200, height=150)
Chart(df).mark_point(size=1).encode(x='AAPL',y='OIL', column='year').configure_cell(width=140, height=70).configure_facet_cell(strokeWidth=0)
Chart(df).mark_point(size=1).encode(x='AAPL',y='OIL', color='year:N').configure_cell(width=140, height=70)