簡體   English   中英

從列中的唯一值創建一個較小的數據框

[英]create a smaller data frame from unique value from a column

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]})

假設我有這樣一個數據框,並且我想創建一個較小的貓數據框和它們的年齡,並按照順序如何完成這樣的事情

你可以這樣做:

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

如果您只想要年齡和動物列,請執行以下操作:

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

你只需要過濾掉不包含'Cat'的行的rest:

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

僅獲取相關數據('animal' 和 '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 

您可以在此處使用df.query

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

如果你只想要animalage

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM