MultiLevel index to columns : getting value_counts

2019-03-28 13:48发布

问题:

In a very general sense, the problem I am looking to solve is changing one component of a multi-level index into columns. That is, I have a Series that contains a multilevel index and I want the lowest level of the index changed into columns in a dataframe. Here is the actual example problem I'm trying to solve,

Here we can generate some sample data:

foo_choices = ["saul", "walter", "jessee"]
bar_choices = ["alpha", "beta", "foxtrot", "gamma", "hotel", "yankee"]

df = DataFrame([{"foo":random.choice(foo_choices), 
                 "bar":random.choice(bar_choices)} for _ in range(20)])
df.head()

which gives us,

     bar     foo
0    beta    jessee
1    gamma   jessee
2    hotel   saul
3    yankee  walter
4    yankee  jessee
...

Now, I can groupby bar and get value_counts of the foo field,

dfgb = df.groupby('foo')
dfgb['bar'].value_counts()

and it outputs,

foo            
jessee  hotel      4
        gamma      2
        yankee     1
saul    foxtrot    3
        hotel      2
        gamma      1
        alpha      1
walter  hotel      2
        gamma      2
        foxtrot    1
        beta       1

But what I want is something like,

          hotel    beta    foxtrot    alpha    gamma    yankee
foo                        
jessee     1       1       5          4        1        1
saul       0       3       0          0        1        0
walter     1       0       0          1        1        0

My solution was to write the following bit:

for v in df['bar'].unique():
    if v is np.nan: continue
    df[v] = np.nan
    df.ix[df['bar'] == v, v] = 1

dfgb = df.groupby('foo')
dfgb.count()[df['bar'].unique()]

回答1:

I think you want:

dfgb['bar'].value_counts().unstack().fillna(0.)