简体   繁体   English

对于 Pandas Dataframe 有没有办法在保留所有其他值的同时将同一类别显示为一个类别?

[英]For Pandas Dataframe is there a way to display same category together as one while retaining all the other values?

For Pandas Dataframe is there a way to display same category together as one while retaining all the other values in string?对于 Pandas Dataframe 有没有办法将相同的类别一起显示为一个类别,同时保留字符串中的所有其他值?

Assuming I have the following Scenario:假设我有以下场景:

pd.DataFrame({"category": ['Associates', 'Manager', 'Associates', 'Associates', 'Engineer', 'Engineer', 'Manager', 'Engineer'],
              "name": ['Abby', 'Jenny', 'Thomas', 'John', 'Eve', 'Danny', 'Kenny', 'Helen'],
              "email": ['Abby@email.com', 'Jenny@email.com', 'Thomas@email.com', 'John@email.com', 'Eve@email.com', 'Danny@email.com', 'Kenny@email.com', 'Helen@email.com']})

How can I attempt to display the dataframe in a this way?如何尝试以这种方式显示 dataframe?

Output: Output:

category     name     email
Associates   Abby     Abby@email.com
             Thomas   Thomas@email.com
             John     John@email.com
Manager      Jenny    Jenny@email.com
             Kenny    Kenny@email.com
Engineer     Eve      Eve@email.com
             Danny    Danny@email.com
             Helen    Helen@email.com

Any advise, or can it be done with groupby functions?有什么建议,还是可以用 groupby 函数来完成? Thanks!谢谢!

It's not really clear to me what you mean by display .我不太清楚你所说的display是什么意思。 To get a print similar (not exactly) like the one you are showing you don't need .groupby() .要获得与您展示的打印类似(不完全)的打印,您不需要.groupby() Just do做就是了

df = df.set_index(["category", "name"]).sort_index()

and get并得到

                              email
category   name                    
Associates Abby      Abby@email.com
           John      John@email.com
           Thomas  Thomas@email.com
Engineer   Danny    Danny@email.com
           Eve        Eve@email.com
           Helen    Helen@email.com
Manager    Jenny    Jenny@email.com
           Kenny    Kenny@email.com

If you really want to modify the columns, then you could try something like如果您真的想修改列,那么您可以尝试类似

df = df.sort_values(["category", "name"], ignore_index=True)
df.loc[df["category"] == df["category"].shift(), "category"] = ""

to get要得到

     category    name             email
0  Associates    Abby    Abby@email.com
1                John    John@email.com
2              Thomas  Thomas@email.com
3    Engineer   Danny   Danny@email.com
4                 Eve     Eve@email.com
5               Helen   Helen@email.com
6     Manager   Jenny   Jenny@email.com
7               Kenny   Kenny@email.com

For this, you will have two line of codes: First, you need to set both your category and name as index为此,您将有两行代码:首先,您需要将categoryname都设置为索引

df.set_index(['category','name'],inplace=True)

Next, you will use groupby.sum to get your desired output.接下来,您将使用groupby.sum来获得所需的 output。

df.groupby(level=[0,1]).sum()
Out[67]: 
                              email
category   name                    
Associates Abby      Abby@email.com
           John      John@email.com
           Thomas  Thomas@email.com
Engineer   Danny    Danny@email.com
           Eve        Eve@email.com
           Helen    Helen@email.com
Manager    Jenny    Jenny@email.com
           Kenny    Kenny@email.com

For this, you can use groupby() function.为此,您可以使用groupby() function。 Showing below is the sample code.下面显示的是示例代码。

df.groupby(['category','name']).max()

Now the data is in indexed format and will be in the same format that you mentioned, if you want to remove the index, use the below code现在数据为索引格式,并且与您提到的格式相同,如果要删除索引,请使用以下代码

df.groupby(['category','name']).max().reset_index()

暂无
暂无

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

相关问题 如何舍入值只显示在pandas中,同时保留数据框中的原始值? - How to round values only for display in pandas while retaining original ones in the dataframe? 在保留所有列的pandas中获取每个类别的前n个值 - Get top n values per category in pandas retaining all columns 如何获得过滤后的数据框进行计算,同时将原始数据框保留在熊猫中? - How to get the filtered dataframe for calculations while retaining the original one in pandas? 比较一列中的float值与pandas DataFrame中的所有其他列 - Compare float values in one column with all other columns in a pandas DataFrame 遍历pandas数据帧中的每一行,并将所有行值乘以同一数据帧中的行值之一 - Iterate over every row in pandas dataframe and multiply all row values by one of the row values in same dataframe 改组 Pandas DataFrame 中的行,同时保留索引 - Shuffling rows in a Pandas DataFrame while retaining the index 聚合 Function 到 dataframe,同时保留 Pandas 中的行 - Aggregate Function to dataframe while retaining rows in Pandas 熊猫groupby和sum,同时保留其他属性 - Pandas groupby and sum while retaining other attributes 将 Pandas DataFrame 转换为具有相同 class 值的字典 - Convert a Pandas DataFrame to a dictionary with values of same class together 如何用pandas中的其他数据帧的值提取一个数据帧的值? - How to extract values of one dataframe with values of other dataframe in pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM