生成一个数据框,用于计算pandas

时间:2017-01-25 13:20:32

标签: python python-2.7 pandas dataframe

我有一个数据框df

          AID   JID   CID
0         1     A     NaN
1         1     A     NaN
2         1     B     NaN
3         1     NaN   X
4         3     A     NaN
5         4     NaN   NaN
6         4     C     X
7         5     C     Y
8         5     C     X
9         6     A     NaN
10        6     B     NaN

我想计算AID使用JID或CID的次数。

结果数据框应该是这样的,其中索引是AID值,列是CID和JID值:

      A     B     C    X    Y
1     2     1     0    1    0
3     1     0     0    0    0
4     0     0     1    1    0
5     0     0     2    1    1
6     1     1     0    0    0     

我知道如何通过手动循环和计数来完成它。但我想知道更有效的方法是什么?

2 个答案:

答案 0 :(得分:3)

melt然后使用pivot_table

In [80]: d2 = pd.melt(df, id_vars="AID")

In [81]: d2.pivot_table(index="AID", columns="value", values="variable", 
                        aggfunc="count", fill_value=0)
Out[81]: 
value  A  B  C  X  Y
AID                 
1      2  1  0  1  0
3      1  0  0  0  0
4      0  0  1  1  0
5      0  0  2  1  1
6      1  1  0  0  0

这是有效的,因为melt将数据框“扁平化”成我们可以更轻松地一起访问这些值的内容,pivot_table正是您想要的聚合类型:

In [90]: pd.melt(df, "AID")
Out[90]: 
   AID variable value
0    1      JID     A
1    1      JID     A
2    1      JID     B
3    1      JID   NaN
4    3      JID     A
[... skipped]
17    4      CID     X
18    5      CID     Y
19    5      CID     X
20    6      CID   NaN
21    6      CID   NaN

答案 1 :(得分:1)

您可以先stack创建Series,然后groupby创建df = df.set_index('AID').stack().groupby(level=0).value_counts().unstack(1, fill_value=0) print (df) A B C X Y AID 1 2 1 0 1 0 3 1 0 0 0 0 4 0 0 1 1 0 5 0 0 2 1 1 6 1 1 0 0 0 。最后重塑value_counts

 if (videoPlaying == true) {

            videoView.isHidden = false
            let targetTime:CMTime = CMTimeMake(0, 1)
            videoPlayer.seek(to: targetTime)
            videoPlayer.pause()
            // audioplayer = nil
            playerLayer.removeFromSuperlayer()
            playerLayer=AVPlayerLayer(player: videoPlayer)
            playerLayer.frame = CGRect(x:0 , y: 75 , width: videoView.frame.size.width , height:  UIScreen.main.bounds.height-170)
            playerLayer.backgroundColor = UIColor.black.cgColor
            playerLayer.videoGravity = AVLayerVideoGravityResize
            videoView.layer.addSublayer(playerLayer)
            let url = NSURL(string: videoString)
            let playerItem1 = AVPlayerItem(url: url! as URL)
            videoPlayer.replaceCurrentItem(with: playerItem1)

            videoPlayer.play()

            AppDelegate.shared().showLoading()
            // Setting slider max value
            let duration : CMTime = videoPlayer.currentItem!.asset.duration
            let seconds : Float64 = CMTimeGetSeconds(duration)
            videoSlider.maximumValue = Float(seconds)
            videoPlayButton.setImage(UIImage(named: "Pause"), for: .normal)
            videoPlaying = true
            //Adding Default Periodic Observer On Player
            videoPlayer.addPeriodicTimeObserver(forInterval: CMTimeMakeWithSeconds(1, 1), queue: DispatchQueue.main) { (CMTime) -> Void in


                if videoPlayer.currentItem?.status == .readyToPlay {
                    AppDelegate.shared().removeLoading()
                    self.videoSlider.value = Float(CMTimeGetSeconds(videoPlayer.currentTime()))
                    let currentTime : Int = Int(CMTimeGetSeconds(videoPlayer.currentTime()))
                    let minutes = currentTime/60
                    let seconds = currentTime - minutes * 60
                    self.durationLabel.text = NSString(format: "%02d:%02d", minutes,seconds) as String

                }
            }

        }
        else{

            videoView.isHidden = true
            let url = NSURL(string: videoString)
            let playerItem = AVPlayerItem(url: url! as URL)
            videoPlayer=AVPlayer(playerItem: playerItem)
            playerLayer=AVPlayerLayer(player: videoPlayer)
            playerLayer.frame = CGRect(x:0 , y: 75 , width: videoView.frame.size.width , height:  UIScreen.main.bounds.height-170)
            playerLayer.backgroundColor = UIColor.black.cgColor
            playerLayer.videoGravity = AVLayerVideoGravityResize
            videoView.layer.addSublayer(playerLayer)

        }