密钥错误: 'Date'


#URL of the dataset
path2="https://drive.google.com/file/d/1XDs3pqZJK7_9Tx28czANgDVEJ_pB_ea8/view?usp=sharing"
#reading the csv file
data2 = pd.read_csv(path2)
#sorting the dataset with respect to Date.
data2 = data2.sort_values('Date')
#Let's have a sneek peek of the dataset.
data2.head()

我试图读取和排序。csv文件,这抛出了错误。

KeyError                                  Traceback (most recent call last)
<ipython-input-7-a50f2c66a46c> in <module>
4 data2 = pd.read_csv(path2)
5 #sorting the dataset with respect to Date.
----> 6 data2 = data2.sort_values('Date')
7 #Let's have a sneek peek of the dataset.
8 data2.head()
2 frames
/usr/local/lib/python3.8/dist-packages/pandas/core/generic.py in _get_label_or_level_values(self, key, axis)
1777             values = self.axes[axis].get_level_values(key)._values
1778         else:
-> 1779             raise KeyError(key)
1780 
1781         # Check for duplicates
KeyError: 'Date'

该怎么办?

我不知道该怎么办


更新!

我删除了'data2-data2.sort_values('Date'),它运行成功,但我收到的输出是这个

我的CSV文件在这里不正确吗?

这是实际的文件

您需要修改url,然后尝试获取数据。您试图获取的结果在获取HTML源代码。而不是csv内容。试试-

path2="https://drive.google.com/file/d/1XDs3pqZJK7_9Tx28czANgDVEJ_pB_ea8/edit"
url='https://drive.google.com/uc?id=' + path2.split('/')[-2]
#reading the csv file
data2 = pd.read_csv(url)
#sorting the dataset with respect to Date.
data2 = data2.sort_values('Date')
#Let's have a sneek peek of the dataset.
data2.head()

相关内容

  • 没有找到相关文章