Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.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 我能把这个数据框转换成线图吗_Python_Pandas_Graph - Fatal编程技术网

Python 我能把这个数据框转换成线图吗

Python 我能把这个数据框转换成线图吗,python,pandas,graph,Python,Pandas,Graph,是否有任何方法可以将这个数据帧转换成一个折线图,其中每个TypeLoc都有一条单独的线,在python中使用pandas测量Y轴上的值,以及x轴上的年份? 我已经尝试了很多方法,但是对python非常陌生,所以没有取得任何进展。 我花了大约5个小时想弄明白,这似乎是最好的提问地点 TESTDATA = StringIO("""DateCode;Value;TypeLoc;Expend_or_Visit; 2014;186;Seaside - beach;Expe

是否有任何方法可以将这个数据帧转换成一个折线图,其中每个TypeLoc都有一条单独的线,在python中使用pandas测量Y轴上的值,以及x轴上的年份? 我已经尝试了很多方法,但是对python非常陌生,所以没有取得任何进展。 我花了大约5个小时想弄明白,这似乎是最好的提问地点

  TESTDATA = StringIO("""DateCode;Value;TypeLoc;Expend_or_Visit;
2014;186;Seaside - beach;Expenditure;
2019;1456;Small town;Expenditure;
2016;4916;All areas;Expenditure;
2018;5474;All areas;Expenditure;
2013;217;Seaside - beach;Expenditure;
2018;6;Seaside - other;Expenditure;
2018;1234;Small town;Expenditure;
2015;230;Seaside resort or town;Expenditure;
2016;302;Seaside resort or town;Expenditure;
2019;359;Seaside resort or town;Expenditure;
2017;458;Seaside resort or town;Expenditure;
2015;3922;All areas;Expenditure;
2014;5020;All areas;Expenditure;
2013;4647;All areas;Expenditure;
2016;1037;Small town;Expenditure;
2013;68;Seaside - other;Expenditure;
2013;1035;Small town;Expenditure;
2017;46;Seaside - beach;Expenditure;
2019;35;Seaside - other;Expenditure;
2015;914;Small town;Expenditure;
2016;32;Seaside - other;Expenditure;
2016;249;Seaside - beach;Expenditure;
2015;36;Seaside - other;Expenditure;
2014;128;Seaside - other;Expenditure;
2017;75;Seaside - other;Expenditure;
2015;81;Seaside - beach;Expenditure;
2019;237;Seaside - beach;Expenditure;
2018;151;Seaside - beach;Expenditure;
2019;457;Village;Expenditure;
2015;308;Village;Expenditure;
2019;3400;City/large town;Expenditure;
2018;3111;City/large town;Expenditure;
2017;1377;Small town;Expenditure;
2017;883;Village;Expenditure;
2014;1398;Small town;Expenditure;
2014;2873;City/large town;Expenditure;
2015;2254;City/large town;Expenditure;
2017;3379;City/large town;Expenditure;
2016;2630;City/large town;Expenditure;
2014;286;Seaside resort or town Expenditure;
2013;2850;City/large town;Expenditure;
2013;206;Seaside resort or town;Expenditure;
2017;778;Rural countryside;Expenditure;
2016;520;Rural countryside;Expenditure;
2017;5995;All areas;Expenditure;
2019;429;Rural countryside;Expenditure;
2015;402;Rural countryside;Expenditure;
2018;228;Seaside resort or town;Expenditure;
2018;502;Rural countryside;Expenditure;
2016;482;Village;Expenditure;
2014;510;Village;Expenditure;
2018;651;Village;Expenditure;
2013;483;Village;Expenditure;
2014;612;Rural countryside;Expenditure;
2019;5777;All areas;Expenditure;
2013;545;Rural countryside;Expenditure;
     """)
链接到完整数据集 (我无法将其读入笔记本,因此手动输入所有内容。) 数据帧:

*试图返回我的代码以获取我从中尝试的内容- 这是我最好的尝试,距离现在还有几英里远:

df.set_index('TypeLoc', inplace=True)

df_expend = df_expend.transpose()

df_expend = df.query('Expend_or_Visit == "Expenditure"')

import matplotlib.pyplot as plt
df_expend.plot(kind='line')
plt.show()

这里有一个方法。您需要先透视数据帧,然后再绘制它:

df = pd.read_csv(StringIO(TESTDATA), sep=";")
df = df[["DateCode", "Value", "TypeLoc"]].pivot(index = "DateCode", columns = "TypeLoc")
df = df.droplevel(0, axis=1)
for col in df.columns:
    plt.plot(df.index, df[col], label =col)
plt.show()
在样本数据有限的情况下,输出为:

要针对较大的数据集(链接中的数据集)运行此代码,请使用:

输出如下。通过更改第二行上的条件,可以为访问创建单独的图表


谢谢你的帮助。它现在给了我一个错误:ValueError:Index包含重复的条目,无法重塑。有什么想法吗?对于每个类型LOC,每个日期代码都有一个条目,具有不同的值。请包括一个大的数据样本(作为文本),以便我可以在我这方面复制该问题。谢谢!我已经编辑了原帖,因为评论中没有太多字符。谢谢。仍然出现错误:索引包含重复项,无法重新形状。我将继续玩,希望能得到它。我无法访问你在谷歌上发布的文档-可能那里有额外的数据?请提供预期的。显示中间结果与预期结果的偏差。发布您的最佳尝试,我们将与您合作。堆栈溢出不是为了替换现有的教程资源。我无法访问google文档。它有密码保护。对不起,应该现在修复。不确定我是否适合做这个。
df = pd.read_csv("scot_stats - Sheet1.csv", header = 1)
df = df[df.Units == "million pounds (GBP)"]

df = df[["DateCode", "Value", "TypeLoc"]].pivot(index = "DateCode", columns = "TypeLoc")
df = df.droplevel(0, axis=1)
for col in df.columns:
    plt.plot(df.index, df[col], label =col)
plt.show()
import pandas as pd
import seaborn as sns
import io

TESTDATA = io.StringIO("""DateCode;Value;TypeLoc;Expend_or_Visit;
2014;186;Seaside - beach;Expenditure;
2019;1456;Small town;Expenditure;
2016;4916;All areas;Expenditure;
2018;5474;All areas;Expenditure;
2013;217;Seaside - beach;Expenditure;
2018;6;Seaside - other;Expenditure;
2018;1234;Small town;Expenditure;
 """)

df = pd.read_csv(TESTDATA, sep =";")

sns.lineplot(x='DateCode', y='Value', hue= 'TypeLoc',data=df)