Link to home
Start Free TrialLog in
Avatar of Newton
Newton

asked on

Python dataframe select only duplicated values.

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
Avatar of Subodh Tiwari (Neeraj)
Subodh Tiwari (Neeraj)
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Newton
Newton

ASKER

Thanks Neeraj. My problem solved.
Great. You're welcome!