pandas意外的set_index行为

时间:2016-02-01 20:52:25

标签: python pandas

--- a/demo/src/main/java/com/google/android/exoplayer/demo/SampleChooserActivity.java
+++ b/demo/src/main/java/com/google/android/exoplayer/demo/SampleChooserActivity.java
@@ -30,6 +28,8 @@ import android.widget.ExpandableListView;
 import android.widget.ExpandableListView.OnChildClickListener;
 import android.widget.TextView;

 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
@@ -44,7 +44,12 @@ public class SampleChooserActivity extends Activity {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.sample_chooser_activity);
     final List<SampleGroup> sampleGroups = new ArrayList<>();
-    SampleGroup group = new SampleGroup("YouTube DASH");
+
+    SampleGroup group = new SampleGroup("test videos");
+    group.addAll(Samples.LOCAL_VIDEOS);
+    sampleGroups.add(group);
+
+    group = new SampleGroup("YouTube DASH");
     group.addAll(Samples.YOUTUBE_DASH_MP4);
     group.addAll(Samples.YOUTUBE_DASH_WEBM);
     sampleGroups.add(group);
diff --git a/demo/src/main/java/com/google/android/exoplayer/demo/Samples.java b/demo/src/main/java/com/google/android/exoplayer/demo/Samples.java
index 9f58528..9e86f99 100644
--- a/demo/src/main/java/com/google/android/exoplayer/demo/Samples.java
+++ b/demo/src/main/java/com/google/android/exoplayer/demo/Samples.java
@@ -248,6 +248,13 @@ import java.util.Locale;
         "http://vod.leasewebcdn.com/bbb.flv?ri=1024&rs=150&start=0", Util.TYPE_OTHER),
   };

+  public static final Sample[] LOCAL_VIDEOS = new Sample[] {
+          new Sample("Some User friendly name of video 1",
+                  "file:///mnt/sdcard/test1.mp4", Util.TYPE_OTHER),
+          new Sample("Some User friendly name of video 2",
+                  "file:///mnt/sdcard/test2.mp4", Util.TYPE_OTHER),
+  };
+
   private Samples() {}

 }

这种行为对我来说相当令人惊讶,因为我期待第二个输出就像

data = [['g1','a',1],['g1','b',2],['g2','b',3],['g2','a',4]]
df = pandas.DataFrame(data=data, columns=['group','name','count'])
print df.set_index(['group','name'])
print df.set_index(['name','group'])

            count
group name       
g1    a         1
      b         2
g2    b         3
      a         4
            count
name group       
a    g1         1
b    g1         2
     g2         3
a    g2         4

1 个答案:

答案 0 :(得分:7)

首先需要对DataFrame进行排序以获得所需的输出:

In [12]: df.sort_values('name').set_index(['name','group'])
Out[12]: 
            count
name group       
a    g1         1
     g2         4
b    g1         2
     g2         3