我无法从我的数据集中绘制两列



我目前正在尝试绘制数据,其中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 中 键错误:"年">

在处理上述异常期间,发生了另一个异常:

回溯(最近一次调用): 文件 "/Users/hannahbeegle/Desktop/Text Files/TeamDataBase.py",第 121 行,在 phillies_data.plot(x="Year", 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 行,第 1973 行,plot_frame **KWDS) 文件"/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/plotting/_core.py",第 1763 行,_plot elif not isinstance(data[x], ABCSeries): 文件 "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/frame.py",第 2927 行,在getitemindexer 中 = 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 中 键错误:"年">

在 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

最新更新