我想将数据分组到我的列“Count”和另一列“State”中。我想输出一个列表列表,每个子集列表只是每个州的计数。
示例输出:[[120,200],[40,20,40],...]
120和200将是让我们说州加利福尼亚州的计数
我尝试了以下内容:
df_new = df[['State']].groupby(['Count']).to_list()
我得到一个keyerror:'count'
回溯:
Traceback (most recent call last):
File "C:\Users\Michael\workspace\UCIIntrotoPythonDA\src\Michael_Madani_week3.py", line 84, in <module>
getStateCountsDF(filepath)
File "C:\Users\Michael\workspace\UCIIntrotoPythonDA\src\Michael_Madani_week3.py", line 81, in getStateCountsDF
df_new = df[['State']].groupby(['Count']).to_list()
File "C:\Users\Michael\Anaconda\lib\site-packages\pandas\core\generic.py", line 3159, in groupby
sort=sort, group_keys=group_keys, squeeze=squeeze)
File "C:\Users\Michael\Anaconda\lib\site-packages\pandas\core\groupby.py", line 1199, in groupby
return klass(obj, by, **kwds)
File "C:\Users\Michael\Anaconda\lib\site-packages\pandas\core\groupby.py", line 388, in __init__
level=level, sort=sort)
File "C:\Users\Michael\Anaconda\lib\site-packages\pandas\core\groupby.py", line 2148, in _get_grouper
in_axis, name, gpr = True, gpr, obj[gpr]
File "C:\Users\Michael\Anaconda\lib\site-packages\pandas\core\frame.py", line 1797, in __getitem__
return self._getitem_column(key)
File "C:\Users\Michael\Anaconda\lib\site-packages\pandas\core\frame.py", line 1804, in _getitem_column
return self._get_item_cache(key)
File "C:\Users\Michael\Anaconda\lib\site-packages\pandas\core\generic.py", line 1084, in _get_item_cache
values = self._data.get(item)
File "C:\Users\Michael\Anaconda\lib\site-packages\pandas\core\internals.py", line 2851, in get
loc = self.items.get_loc(item)
File "C:\Users\Michael\Anaconda\lib\site-packages\pandas\core\index.py", line 1572, in get_loc
return self._engine.get_loc(_values_from_object(key))
File "pandas\index.pyx", line 134, in pandas.index.IndexEngine.get_loc (pandas\index.c:3824)
File "pandas\index.pyx", line 154, in pandas.index.IndexEngine.get_loc (pandas\index.c:3704)
File "pandas\hashtable.pyx", line 686, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12280)
File "pandas\hashtable.pyx", line 694, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12231)
KeyError: 'Count'
我觉得这应该是一行简单的代码,我在这里做错了什么?
答案 0 :(得分:1)
这可以作为一个单行:
import UIKit
import Parse
class ViewController: UIViewController {
@IBOutlet var passwordTextField: UITextField!
@IBOutlet var emailTextField: UITextField!
//@IBOutlet var messageLabel: UILabel!
@IBAction func loginVerifyButton(sender: AnyObject) {
var pwdEntered = passwordTextField.text
var emlEntered = emailTextField.text
if pwdEntered != "" && emlEntered != "" {
// If not empty then yay, do something
func userSignUp() {
var user = PFUser()
user.email = emlEntered
user.password = pwdEntered
user.signUpInBackgroundWithBlock {
(succeeded: Bool, error: NSError?) -> Void in
if let error = error {
let errorString = error.userInfo?["error"] as? NSString
// Show the errorString somewhere and let the user try again.
} else {
// Hooray! Let them use the app now.
}
}
}
}else {
//self.messageLabel.text = "All Fields Required"
}
您应该阅读groupby的文档,了解分组的预期结果以及如何更改(http://pandas.pydata.org/pandas-docs/stable/groupby.html)。