MultiLevel index to columns : getting value_counts as columns in pandas

放肆的年华 提交于 2020-01-01 05:16:10

问题


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.)


来源:https://stackoverflow.com/questions/11971381/multilevel-index-to-columns-getting-value-counts-as-columns-in-pandas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!