获取Pandas中所有属性级别组合的行数

时间:2017-01-03 10:27:07

标签: python-3.x pandas

我有一组数据框的分类变量,每行对应一个产品。我想找到每个属性级别组合的行数,并决定运行以下内容:

att1=list(frame_base.columns.values)
f1=att.groupby(att1,as_index=False).size().rename('counts').to_frame()

att1 是所有属性的列表, f1 似乎没有提供正确的值< strong> f1.counts.sum() 在群组之前不等于 len(f1) 。为什么不这项工作?

1 个答案:

答案 0 :(得分:1)

一个可能的问题是att行,但可能有拼写错误 - 需要frame_base而不是att = pd.DataFrame({'A':[1,1,3,np.nan], 'B':[1,1,6,np.nan], 'C':[2,2,9,np.nan], 'D':[1,1,5,np.nan], 'E':[1,1,6,np.nan], 'F':[1,1,3,np.nan]}) print (att) A B C D E F 0 1.0 1.0 2.0 1.0 1.0 1.0 1 1.0 1.0 2.0 1.0 1.0 1.0 2 3.0 6.0 9.0 5.0 6.0 3.0 3 NaN NaN NaN NaN NaN NaN att1=list(att.columns.values) f1=att.groupby(att1).size().reset_index(name='counts') print (f1) A B C D E F counts 0 1.0 1.0 2.0 1.0 1.0 1.0 2 1 3.0 6.0 9.0 5.0 6.0 3.0 1

- (IBAction)loginbutton:(id)sender
{
    @try {
        if([[_email text] isEqualToString:@""] || [[_password text] isEqualToString:@""] ) {
            [self alertFailed:@"Please enter both Username and Password" :@"Login Failed!"];
        } else {
            NSString *post =[[NSString alloc] initWithFormat:@"email=%@&password=%@",[_email text],[_password text]];
            NSLog(@"PostData: %@",post);
            NSURL *url=[NSURL URLWithString:@"http://localhost/login.php"];
            NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

            NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];

            NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
            [request setURL:url];
            [request setHTTPMethod:@"POST"];
            [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
            [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
            [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
            [request setHTTPBody:postData];
            //[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
            NSError *error = [[NSError alloc] init];
            NSHTTPURLResponse *response = nil;
            NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

            NSLog(@"Response code: %ld", (long)[response statusCode]);
            if ([response statusCode] >=200 && [response statusCode] <300) {
                NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
                NSLog(@"Response ==> %@", responseData);

                SBJsonParser *jsonParser = [SBJsonParser new];
                NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:responseData error:nil];
                NSLog(@"%@",jsonData);
                NSInteger success = [(NSNumber *) [jsonData objectForKey:@"success"] integerValue];
                NSLog(@"%ld",(long)success);
                if(success == 1) {
                    NSLog(@"Login SUCCESS");
                    [self alertStatus:@"Logged in Successfully." :@"Login Success!"]; 
                } else {
                    NSString *error_msg = (NSString *) [jsonData objectForKey:@"error_message"];
                    [self alertFailed:error_msg :@"Login Failure! Correct your credentials"];
                } 
            } else {
                if (error) NSLog(@"Error: %@", error);
                [self alertFailed:@"Connection Failed" :@"Login Failed!"];
            }
        }
    }
    @catch (NSException * e) {
        NSLog(@"Exception: %@", e);
        [self alertStatus:@"Login Failed." :@"Login Failed!"];
    }
}//End click function