[英]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为此,您将有两行代码:首先,您需要将
category
和name
都设置为索引
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.