troubleshooting Question

Python dataframe select only duplicated values.

Avatar of Newton
Newton asked on
Python
3 Comments1 Solution113 ViewsLast Modified:
I want to extract only those rows where specific column values are duplicated.

For example, my source file is

File 1 : CountryA.txt
fruits|count
apple|100
orange|200
orange|245
grapes|230

I need following output
output1.txt
fruits|count
orange|200
orange|245

My code is, is this the correct way of doing this ?

Df1 = pd.read_csv('CountryA.txt',sep="|")
Df1 = Df1[Df1['fruits'].duplicated()]
Df1.to_csv('output1.txt',sep="|")
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
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 3 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