[英]Creating a Summary from a Dataframe
I've written python code that generates a two column dataframe containing a list of our staff, and the team they're assigned to that day (ie column A: staff member, column B: team).我编写了 python 代码,该代码生成两列 dataframe 包含我们的员工列表以及他们当天分配的团队(即 A 列:员工,B 列:团队)。 From this
dataframe
, I'd like to generate a .txt
file or a string formatted as follows:从此
dataframe
,我想生成一个.txt
文件或格式如下的字符串:
Team 1 (line break) staff member (line break) staff member (line break) staff member (line break) Team 2 (line break) staff member (line break)团队 1 (换行)职员(换行)职员(换行)职员(换行)团队 2 (换行)职员(换行)
etc.等等
I have tried using df.pivot()
to achieve something similar, but didn't have much luck.我曾尝试使用
df.pivot()
来实现类似的目标,但运气不佳。 Apologies if this is a very basic question, I'm learning all of this as I go.抱歉,如果这是一个非常基本的问题,我正在学习所有这些,因为我是 go。 Thank you!
谢谢!
Desired Output:所需的 Output:
Winning Team获胜团队
Dave戴夫
Alex亚历克斯
Sabrina萨布丽娜
Losing Team失败的球队
John约翰
Douglas道格拉斯
import pandas as pd
df = pd.DataFrame({"Staff":["Julie", "Max", "Sam", "Paula", "Mary"],
"Team": ["Team 1", "Team 2", "Team 3", "Team 2", "Team 1"]})
grouped = df.groupby("Team")
result_string = ""
for team, group in grouped:
result_string += (team + "\n")
for person in group["Staff"]:
result_string += ("\t" + person + "\n")
print(f'Dataframe: \n{df}')
print('______________\nResult String:')
print(result_string)
PRINTS THE FOLLOWING:打印以下内容:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.