简体   繁体   English

Groupby 在 Pandas Dataframe 与多索引

[英]Groupby In Pandas Dataframe with MultiIndexing

PS: GroupBy with column as name PS:以列为name的 GroupBy

I have tried creating DataFrame with MultiIndexing:我尝试使用 MultiIndexing 创建 DataFrame:

import pandas as pd

df = [ [ 'las_00', '6', '3', '3', 'a', '1.03', '1.11', '1.11' ],
       [ 'las_01', '6', '3', '3', 'b', '1.03', '1.11', '1.11' ],
       [ 'las_02', '6', '3', '3', 'c', '1.03', '1.11', '1.11' ],
       [ 'las_03', '6', '3', '3', 'a', '1.03', '1.11', '1.11' ],
       [ 'las_03', '6', '3', '3', 'b', '1.03', '1.11', '1.11' ]
    ]


new_df = pd.DataFrame( df , columns = [ 'name, name', 'transactionCount, totalCount', 'transactionCount, passCount', 'transactionCount, failCount', 'status, failPerc', 'status, mean',
                    'status, perc90', 'status, max' ] )


a = new_df.columns.str.split( ', ', expand=True ).values

new_df.columns = pd.MultiIndex.from_tuples( [ ( ' ', x[ 0 ] ) if pd.isnull( x[ 1 ] ) else x for x in a])


Resultant dataframe is:结果 dataframe 是:

     name               transactionCount                       status
     name       totalCount passCount failCount failPerc  mean perc90   max
0  las_00                6         3         3        a  1.03   1.11  1.11
1  las_01                6         3         3        b  1.03   1.11  1.11
2  las_02                6         3         3        c  1.03   1.11  1.11
3  las_03                6         3         3        a  1.03   1.11  1.11
4  las_03                6         3         3        b  1.03   1.11  1.11

Now I want to use GroupBy with name I tried using level but not getting how to use column name .现在我想使用带有名称的 GroupBy我尝试使用level但没有得到如何使用 column name Could anyone help with this!任何人都可以帮助解决这个问题! Thanks谢谢

Try this:尝试这个:

new_df.groupby(('name','name'))

Also, you can groupby dataframe column slices:此外,您可以按 dataframe 列切片进行分组:

new_df.groupby(new_df.columns[0])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM