Reputation: 45
animals = pd.DataFrame({'animal': ['Dog','Cat','Cat','Cat','Dog','Dog','Cat','Dog','Cat','Cat','Dog','Dog','Cat'],
'age':[2,1,5,7,5,3,4,6,6,9,3,2,10],
'weight':[10,4,3,15,12,5,6,3,7.1,10,12,6,4],
'length':[1,0.45,0.49,0.50,1.2,1.16,0.40,1.2,0.45,0.50,0.75,1.1,0.43]})
Suppose I have such a data frame and I want to create a smaller data frame of cats and their age in acnding order how such a thing can be done
Upvotes: 1
Views: 87
Reputation: 20669
You can use df.query
here.
df.query("animal=='Cat'").sort_values('age')
# Alternative
# df.query("animal.eq('Cat')").sort_values('age')
animal age weight length
1 Cat 1 4.0 0.45
6 Cat 4 6.0 0.40
2 Cat 5 3.0 0.49
8 Cat 6 7.1 0.45
3 Cat 7 15.0 0.50
9 Cat 9 10.0 0.50
12 Cat 10 4.0 0.43
If you only want animal
and age
df[['animal', 'age']].query("animal=='Cat'").sort_values('age')
animal age
1 Cat 1
6 Cat 4
2 Cat 5
8 Cat 6
3 Cat 7
9 Cat 9
12 Cat 10
Upvotes: 1
Reputation: 5745
you just need to filter out the rest of the rows which does not contains 'Cat':
animals = pd.DataFrame({'animal': ['Dog','Cat','Cat','Cat','Dog','Dog','Cat','Dog','Cat','Cat','Dog','Dog','Cat'],
'age':[2,1,5,7,5,3,4,6,6,9,3,2,10],
'weight':[10,4,3,15,12,5,6,3,7.1,10,12,6,4],
'length':[1,0.45,0.49,0.50,1.2,1.16,0.40,1.2,0.45,0.50,0.75,1.1,0.43]})
animals = animals[animals['animal'] == 'Cat'].sort_values(['age'])
animals
>>>
animal age weight length
1 Cat 1 4.0 0.45
6 Cat 4 6.0 0.40
2 Cat 5 3.0 0.49
8 Cat 6 7.1 0.45
3 Cat 7 15.0 0.50
9 Cat 9 10.0 0.50
12 Cat 10 4.0 0.43
to get only relevant data ('animal' and 'age' ):
animals[['animal','age']]
>>> animal age
1 Cat 1 4.0
6 Cat 4 6.0
2 Cat 5 3.0
8 Cat 6 7.1
3 Cat 7 15.0
9 Cat 9 10.0
12 Cat 10 4.0
Upvotes: 1
Reputation: 61910
You could do:
res = animals[animals['animal'].eq('Cat')].sort_values(by='age')
print(res)
Output
animal age weight length
1 Cat 1 4.0 0.45
6 Cat 4 6.0 0.40
2 Cat 5 3.0 0.49
8 Cat 6 7.1 0.45
3 Cat 7 15.0 0.50
9 Cat 9 10.0 0.50
12 Cat 10 4.0 0.43
If you just want the age and and animal columns, do:
res = animals[animals['animal'].eq('Cat')].filter(items=['animal', 'age']).sort_values(by='age')
print(res)
Output
animal age
1 Cat 1
6 Cat 4
2 Cat 5
8 Cat 6
3 Cat 7
9 Cat 9
12 Cat 10
Upvotes: 3