troubleshooting Question

Groupby in python

Avatar of Newton
Newton asked on
Python
4 Comments1 Solution120 ViewsLast Modified:
How to use groupby function in python

Below is my source file

File1
Fruits|country|count
Apple|countryA|100
Orange|countryA|20
Apple|countryB|200
Orange|countryB|12

I want to group records based on country and I want below output.

Output1
Country|Fruits|count
countryA|Apple|100
countryB|Apple|200
countryA|Orange|20
countryB|Orange|12

I tried below code but it is not working.

Df = pd.read_csv('file1.txt, sep="|")
Grouped = Df.groupby('Country', axis='Columns')
Grouped.to_csv('output1.txt', sep="|",index=False)

But I am getting error. What is the correct way for group data in python.
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 4 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros