我不知道为什么以下代码不起作用。我试图掩盖大熊猫数组,就像我的顾问给我看的那样,但不管我做什么都会一直出错。我会以某种方式将pandas数据帧与numpy数组混合在一起吗?我查看了如何在另一个线程中执行此操作,我似乎在做同样的事情。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import os
data_dir = "./data/"
incomes_file = "incomes.csv"
incomes = pd.read_csv(os.path.join(data_dir,incomes_file))
print incomes.County_Name
print incomes.County_Name[str(incomes.County_Name).isupper() != True]
我得到了这个输出:
0 ALABAMA
1 Autauga County
2 Baldwin County
3 Barbour County
4 Bibb County
5 Blount County
6 Bullock County
7 Butler County
8 Calhoun County
9 Chambers County
10 Cherokee County
11 Chilton County
12 Choctaw County
13 Clarke County
14 Clay County
...
3178 Hot Springs County
3179 Johnson County
3180 Laramie County
3181 Lincoln County
3182 Natrona County
3183 Niobrara County
3184 Park County
3185 Platte County
3186 Sheridan County
3187 Sublette County
3188 Sweetwater County
3189 Teton County
3190 Uinta County
3191 Washakie County
3192 Weston County
Name: County_Name, Length: 3193, dtype: object
Traceback (most recent call last):
File "/Users/Avi/Desktop/Average Income By County/data_analysis.py", line 13, in <module>
print incomes.County_Name[str(incomes.County_Name).isupper()]
File "/Users/Avi/anaconda/lib/python2.7/site-packages/pandas/core/series.py", line 479, in __getitem__
result = self.index.get_value(self, key)
File "/Users/Avi/anaconda/lib/python2.7/site-packages/pandas/core/index.py", line 1171, in get_value
return self._engine.get_value(s, k)
File "index.pyx", line 97, in pandas.index.IndexEngine.get_value (pandas/index.c:2987)
File "index.pyx", line 105, in pandas.index.IndexEngine.get_value (pandas/index.c:2802)
File "index.pyx", line 146, in pandas.index.IndexEngine.get_loc (pandas/index.c:3495)
File "index.pyx", line 357, in pandas.index.Int64Engine._check_type (pandas/index.c:6813)
KeyError: False
[Finished in 1.4s with exit code 1]
[shell_cmd: python -u "/Users/Avi/Desktop/Average Income By County/data_analysis.py"]
[dir: /Users/Avi/Desktop/Average Income By County]
[path: /usr/bin:/bin:/usr/sbin:/sbin]
答案 0 :(得分:1)
试试这个:
incomes[ incomes.Country_Name.apply(lambda x: x.isupper()) != True ]
您无法在所有DataFrame上使用str()
- (下次使用incomes.Country_Name.str
)
但是没有incomes.Country_Name.str.isupper()
可以做出更简单的解决方案。