pandas group按选择列

时间:2016-07-08 05:14:40

标签: pandas group-by

我使用Cloudera VM 5.2.0 pandas 0.18.0。

我有以下数据

var width: CGFloat
var height: CGFloat
var inputImage: UIImage

    // input image to be composited over new image as example
// create a new bitmap image context at the device resolution (retina/non-retina)
UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height), true, 0.0)
    // get context
var context: CGContextRef = UIGraphicsGetCurrentContext()
    // push context to make it current 
    // (need to do this manually because we are not drawing in a UIView)
UIGraphicsPushContext(context)
// drawing code comes here- look at CGContext reference
// for available operations
// this example draws the inputImage into the context
inputImage.drawInRect(CGRectMake(0, 0, width, height))
// pop context 
UIGraphicsPopContext()
    // get a UIImage from the image context- enjoy!!!
var outputImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
// clean up drawing environment
UIGraphicsEndImageContext()

我想为字段时间戳

执行分组操作
adclicksDF = pd.read_csv('/home/cloudera/Eglence/ad-clicks.csv',
               parse_dates=['timestamp'],
       skipinitialspace=True).assign(adCount=1)

adclicksDF.head(n=5)
Out[65]: 
            timestamp  txId  userSessionId  teamId  userId  adId   adCategory  \
0 2016-05-26 15:13:22  5974           5809      27     611     2  electronics   
1 2016-05-26 15:17:24  5976           5705      18    1874    21       movies   
2 2016-05-26 15:22:52  5978           5791      53    2139    25    computers   
3 2016-05-26 15:22:57  5973           5756      63     212    10      fashion   
4 2016-05-26 15:22:58  5980           5920       9    1027    20     clothing   

   adCount  
0        1  
1        1  
2        1  
3        1  
4        1  

我想在agrupado中添加更多列adCategory,idUser。 我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

每个userId的{​​{1}}和adCategory都有多个值,因此group会聚集:

在此示例中,最后两个日期时间已更改以获得更好的输出

join
print (adclicksDF)
            timestamp  txId  userSessionId  teamId userId  adId   adCategory  \
0 2016-05-26 15:13:22  5974           5809      27    611     2  electronics   
1 2016-05-26 15:17:24  5976           5705      18   1874    21       movies   
2 2016-05-26 15:22:52  5978           5791      53   2139    25    computers   
3 2016-05-26 16:22:57  5973           5756      63    212    10      fashion   
4 2016-05-26 16:22:58  5980           5920       9   1027    20     clothing   

   adCount  
0        1  
1        1  
2        1  
3        1  
4        1