csv 密钥错误:“日期”

qf9go6mv  于 2022-12-27  发布在  其他
关注(0)|答案(1)|浏览(114)
#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 '),它成功运行,但收到的输出如下
is my csv file incorrect here ?
this is the actual file

whlutmcx

whlutmcx1#

您需要修改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()

相关问题