-1

I am working on excel sheet:

animal name    age
dog    puppy   1
dog    doggy   2
dog    snooppy 3
cat    pussy   1
pig    piggy   1
pig    cutty   2
rabit  robby   1
rabbit bunny   2

Here I should check if animal age is 1 should delete that row and print next row and remove duplicates if there are no duplicates, should print that row and this output should print in other excel sheet.

Can any help me out of this?

import 
imatplotlib.pyplot as plt
import pandas as pd

data = pd.read_excel(r"C:\Users\c_ssaiva\Desktop\sampladata.xlsx")
for index,row in data.iterrows():
print(index,row['animal'],row['name'],row['age'])
for j,row in data.iterrows():

 if a[i] == a[j]:
 if a[i] == 1
 print a[j]
 else:
 print (a[i])
 df = data
 df.to_excel(r"C:\Users\c_ssaiva\Desktop\selcol.xlsx")          
Shravya Vaggu
  • 41
  • 1
  • 6

2 Answers2

0

Hej Shrvya. Pandas is awesome and can do all you are asking without loops :) You could do that in one line

df = data[data['age'] != 1].drop_duplicates()

We have made a new df that removes all records where 'age' != 1 and then we drop duplicates :)

I am not sure what is the aim of printing values out. Why do you want to print values on screen?

Prayson W. Daniel
  • 14,191
  • 4
  • 51
  • 57
0

Or you can use this simple:

df.drop_duplicates(subset=['column'], keep='first', inplace=True)