Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从列的唯一值创建较小的数据框_Python_Pandas_Dataframe - Fatal编程技术网

Python 从列的唯一值创建较小的数据框

Python 从列的唯一值创建较小的数据框,python,pandas,dataframe,Python,Pandas,Dataframe,假设我有这样一个数据框,我想创建一个较小的猫数据框,并按顺序显示它们的年龄,以确定如何完成这项工作您可以做: 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,

假设我有这样一个数据框,我想创建一个较小的猫数据框,并按顺序显示它们的年龄,以确定如何完成这项工作

您可以做:

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

如果只需要年龄和动物栏,请执行以下操作:

   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
输出

你可以做:

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

如果只需要年龄和动物栏,请执行以下操作:

   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
输出


您只需过滤掉不包含“Cat”的其余行:

   animal  age
1     Cat    1
6     Cat    4
2     Cat    5
8     Cat    6
3     Cat    7
9     Cat    9
12    Cat   10
仅获取“动物”和“年龄”相关数据:

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

您只需过滤掉不包含“Cat”的其余行:

   animal  age
1     Cat    1
6     Cat    4
2     Cat    5
8     Cat    6
3     Cat    7
9     Cat    9
12    Cat   10
仅获取“动物”和“年龄”相关数据:

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
您可以在这里使用df.query

如果你只想要动物和年龄

您可以在这里使用df.query

如果你只想要动物和年龄


我能说什么?你太棒了,跑得更快了:我能说什么?你真是太棒了,速度也更快了:
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