【问题标题】:I am not able to plot two columns from my data set我无法从我的数据集中绘制两列
【发布时间】:2019-11-01 01:10:05
【问题描述】:

我目前正在尝试绘制数据,其中x 变量是年份,y 变量是费城费城人队在一个赛季中获胜的次数。我尝试了多种方法从我的数据集中绘制这两个变量,但是没有任何效果。以下是我尝试的最后一个选项。

我文件的第一列是年份,第三列是获胜次数(也就是第 0 列和第 2 列)。

我尝试将 x 和 y 设置为列,下面是我最近尝试过的。

import csv
import numpy
import random
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize']=(10,6)

phillies_data = 
pd.read_csv('/Users/hannahbeegle/Desktop/Teams/PHILLIEScsv.csv', 
header = None)

phillies_data.plot(x='Year',y='W')
plt.xlabel('Year')
plt.ylabel('Wins')
plt.title('Amount of Wins in Phillies History (1871-2018)')
plt.xlim(1870, 2020)
plt.ylim(0, 170)

plt.show()

错误信息:

Traceback (most recent call last):

文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/indexes/base.py”,第 2657 行,在 get_loc 返回 self._engine.get_loc(key) 文件“pandas/_libs/index.pyx”,第 108 行,在 pandas._libs.index.IndexEngine.get_loc 文件“pandas/_libs/index.pyx”,第 129 行,在 pandas._libs.index.IndexEngine.get_loc 文件“pandas/_libs/index_class_helper.pxi”,第 91 行,在 pandas._libs.index.Int64Engine._check_type KeyError: '年份'

在处理上述异常的过程中,又发生了一个异常:

Traceback(最近一次调用最后一次): 文件“/Users/hannahbeegle/Desktop/Text Files/TeamDataBase.py”,第 121 行,在 phillies_data.plot(x="年", y="W") 调用中的文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/plotting/_core.py”,第 2942 行 sort_columns=sort_columns, **kwds) 文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/plotting/_core.py”,第 1973 行,在 plot_frame **kwds) 文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/plotting/_core.py”,第 1763 行,在 _plot elif 不是实例(数据 [x],ABCSeries): getitem 中的文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/frame.py”,第 2927 行 索引器 = self.columns.get_loc(key) 文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/indexes/base.py”,第 2659 行,在 get_loc 返回 self._engine.get_loc(self._maybe_cast_indexer(key)) 文件“pandas/_libs/index.pyx”,第 108 行,在 pandas._libs.index.IndexEngine.get_loc 文件“pandas/_libs/index.pyx”,第 129 行,在 pandas._libs.index.IndexEngine.get_loc 文件“pandas/_libs/index_class_helper.pxi”,第 91 行,在 pandas._libs.index.Int64Engine._check_type KeyError: '年份'

【问题讨论】:

  • 参数有误。 xy 参数在您的数据框中采用 列的名称,而不是列本身。 df.plot(x="column1", y="column3")
  • 尝试使用 .iloc untead of .loc 吗?
  • @ImportanceOfBeingErnest 我按照你说的做了,得到了我在上面编辑的这个错误
  • 我们不知道数据框的样子。请参阅minimal reproducible exampleHow to make good reproducible pandas examples
  • @ImportanceOfBeingErnest file:///Users/hannahbeegle/Desktop/Scr​​een%20Shot%202019-06-18%20at%2012.30.27%20PM.png

标签: python pandas matplotlib plot


【解决方案1】:

在您的 jupyter 会话中尝试以下示例:

df = pd.DataFrame(np.random.randn(1000, 2), columns=['BB', 'CC']).cumsum()
df['AA'] = pd.Series(list(range(len(df))))
df.plot(x='AA', y='BB')

您将看到 BBAA 的图(即一列一次增加 1 步,而不绘制其他列。 我希望这可以很容易地转化为你的例子。 如果您需要检查列名,请尝试:

df.columns

【讨论】:

    猜你喜欢
    • 2019-10-20
    • 1970-01-01
    • 1970-01-01
    • 2014-01-09
    • 1970-01-01
    • 1970-01-01
    • 2021-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多