python - 在 Pandas 中使用 groupby 函数时如何解决“keyerror”?
问题描述
我正在尝试将我的数据集与“驱动轮”、“车身样式”和“价格”进行分组。我得到了关键错误。我的代码是。(我已经进口了熊猫)
url="https://archive.ics.uci.edu/ml/machine-learning-databases/autos/imports-85.data"
df=pd.read_csv(url)
df_test=df['drive-wheels:','body-style:','price:']
df_grp=df_test.groupby(['drive-wheels:','body-style:'], as_index= False).mean()
df_pivot=df_grp.pivot(index='drive-wheels:',columns='body-style')
我收到了这个错误。我尝试了各种方法,例如删除列之间的空间。我是熊猫的新手。所以如果有人可以帮助我,我会很高兴
D:\SOFTWARE\IllustratorPortable\anc\lib\site-packages\pandas\core\indexes\base.py in
get_loc(self,key, method, tolerance)
2601 try:
-> 2602 return self._engine.get_loc(key)
2603 except KeyError:
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: ('drive-wheels:', 'body-style:', 'price:')
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
<ipython-input-8-a14bda9f1cf1> in <module>
1 url="https://archive.ics.uci.edu/ml/machine-learning-databases/autos/imports-85.data"
2 df=pd.read_csv(url)
----> 3 df_test=df['drive-wheels:','body-style:','price:']
4 df_grp=df_test.groupby(['drive-wheels:','body-style:'], as_index= False).mean()
5 df_pivot=df_grp.pivot(index='drive-wheels:',columns='body-style')
D:\SOFTWARE\IllustratorPortable\anc\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
2915 if self.columns.nlevels > 1:
2916 return self._getitem_multilevel(key)
-> 2917 indexer = self.columns.get_loc(key)
2918 if is_integer(indexer):
2919 indexer = [indexer]
D:\SOFTWARE\IllustratorPortable\anc\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
2602 return self._engine.get_loc(key)
2603 except KeyError:
-> 2604 return self._engine.get_loc(self._maybe_cast_indexer(key))
2605 indexer = self.get_indexer([key], method=method, tolerance=tolerance)
2606 if indexer.ndim > 1 or indexer.size > 1:
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: ('drive-wheels:', 'body-style:', 'price:')
解决方案
该文件不包含标题。
Attribute: Attribute Range:
------------------ -----------------------------------------------
1. symboling: -3, -2, -1, 0, 1, 2, 3.
2. normalized-losses: continuous from 65 to 256.
3. make: alfa-romero, audi, bmw, chevrolet, dodge, honda,
isuzu, jaguar, mazda, mercedes-benz, mercury,
mitsubishi, nissan, peugot, plymouth, porsche,
renault, saab, subaru, toyota, volkswagen, volvo
4. fuel-type: diesel, gas.
5. aspiration: std, turbo.
6. num-of-doors: four, two.
7. body-style: hardtop, wagon, sedan, hatchback, convertible.
8. drive-wheels: 4wd, fwd, rwd.
9. engine-location: front, rear.
10. wheel-base: continuous from 86.6 120.9.
11. length: continuous from 141.1 to 208.1.
12. width: continuous from 60.3 to 72.3.
13. height: continuous from 47.8 to 59.8.
14. curb-weight: continuous from 1488 to 4066.
15. engine-type: dohc, dohcv, l, ohc, ohcf, ohcv, rotor.
16. num-of-cylinders: eight, five, four, six, three, twelve, two.
17. engine-size: continuous from 61 to 326.
18. fuel-system: 1bbl, 2bbl, 4bbl, idi, mfi, mpfi, spdi, spfi.
19. bore: continuous from 2.54 to 3.94.
20. stroke: continuous from 2.07 to 4.17.
21. compression-ratio: continuous from 7 to 23.
22. horsepower: continuous from 48 to 288.
23. peak-rpm: continuous from 4150 to 6600.
24. city-mpg: continuous from 13 to 49.
25. highway-mpg: continuous from 16 to 54.
26. price: continuous from 5118 to 45400.
您可以使用 iloc
df_test = df.iloc[[7,6,25]]
或设置列
df.columns = ['one', 'two', 'three']
推荐阅读
- watchkit - Apple Watch 上的 Scratchy AVAudioengine
- php - Laravel Collection groupBy() 不能使用 2 个值
- android - 应用程序未在发布模式下构建
- android - 来自属性的 BottomNavigationView 的自定义背景颜色
- swift - 在 UIImage 上添加 UIView - swift - 以编程方式
- java - 如何从java中的字符串中删除尖锐的重音?
- shopify - Shopify - 获取 Product Variant 的所有图像 - GraphQl
- json - 不能在 49 型和 31 型之间进行强制转换
- r - R:匹配两个数据框并在一个中创建新列
- javascript - Angular7-按字母顺序排序选择下拉选项