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). From this dataframe
, I'd like to generate a .txt
file or a string formatted as follows:
Team 1 (line break) staff member (line break) staff member (line break) staff member (line break) Team 2 (line break) staff member (line break)
etc.
I have tried using df.pivot()
to achieve something similar, but didn't have much luck. Apologies if this is a very basic question, I'm learning all of this as I go. Thank you!
Desired 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:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.