📌  相关文章
📜  计算 Pandas 数据框中的所有行或满足某些条件的行

📅  最后修改于: 2022-05-13 01:55:36.401000             🧑  作者: Mango

计算 Pandas 数据框中的所有行或满足某些条件的行

让我们看看如何计算 Dataframe 中的所有行数或 Pandas 中满足条件的行数。

1) 使用 Dataframe.shape 计算 Pandas Dataframe 中的所有行。

Dataframe.shape返回数据框/系列的形状(行、列)的元组。

让我们创建一个熊猫数据框。

# import pandas library as pd
import pandas as pd
  
# List of Tuples
students = [('Ankit', 22, 'Up', 'Geu'),
           ('Ankita', 31, 'Delhi', 'Gehu'),
           ('Rahul', 16, 'Tokyo', 'Abes'),
           ('Simran', 41, 'Delhi', 'Gehu'),
           ('Shaurya', 33, 'Delhi', 'Geu'),
           ('Harshita', 35, 'Mumbai', 'Bhu' ),
           ('Swapnil', 35, 'Mp', 'Geu'),
           ('Priya', 35, 'Uk', 'Geu'),
           ('Jeet', 35, 'Guj', 'Gehu'),
           ('Ananya', 35, 'Up', 'Bhu')
            ]
  
# Create a DataFrame object from
# list of tuples with columns
# and indices.
details = pd.DataFrame(students, columns =['Name', 'Age', 
                                           'Place', 'College'],
                        index =['a', 'b', 'c', 'd', 'e',
                                'f', 'g', 'i', 'j', 'k'])
  
details

输出:

熊猫-cunr-rows-1

代码:计算所有行

# import pandas library as pd
import pandas as pd
  
# List of Tuples
students = [('Ankit', 22, 'Up', 'Geu'),
           ('Ankita', 31, 'Delhi', 'Gehu'),
           ('Rahul', 16, 'Tokyo', 'Abes'),
           ('Simran', 41, 'Delhi', 'Gehu'),
           ('Shaurya', 33, 'Delhi', 'Geu'),
           ('Harshita', 35, 'Mumbai', 'Bhu' ),
           ('Swapnil', 35, 'Mp', 'Geu'),
           ('Priya', 35, 'Uk', 'Geu'),
           ('Jeet', 35, 'Guj', 'Gehu'),
           ('Ananya', 35, 'Up', 'Bhu')
            ]
  
# Create a DataFrame object from
# list of tuples with columns
# and indices.
details = pd.DataFrame(students, columns =['Name', 'Age',
                                           'Place', 'College'],
                        index =['a', 'b', 'c', 'd', 'e', 
                                'f', 'g', 'i', 'j', 'k'])
  
# 0th index of tuple returned by shape
# attribute give the number
# of rows in a given dataframe
num_rows = details.shape[0]
  
print('Number of Rows in given dataframe : ',
      num_rows)

输出:

Number of Rows in given dataframe :  10

2) 使用 Dataframe.index 计算 Pandas Dataframe 中的所有行。

Dataframe.index属性给出了一系列索引或行标签。

代码:

import pandas as pd
  
# List of Tuples
students = [('Ankit', 22, 'Up', 'Geu'),
           ('Ankita', 31, 'Delhi', 'Gehu'),
           ('Rahul', 16, 'Tokyo', 'Abes'),
           ('Simran', 41, 'Delhi', 'Gehu'),
           ('Shaurya', 33, 'Delhi', 'Geu'),
           ('Harshita', 35, 'Mumbai', 'Bhu' ),
           ('Swapnil', 35, 'Mp', 'Geu'),
           ('Priya', 35, 'Uk', 'Geu'),
           ('Jeet', 35, 'Guj', 'Gehu'),
           ('Ananya', 35, 'Up', 'Bhu')
            ]
  
# Create a DataFrame object from
# list of tuples with columns
# and indices.
details = pd.DataFrame(students, columns =['Name', 'Age',
                                           'Place', 'College'],
                        index =['a', 'b', 'c', 'd', 'e',
                                'f', 'g', 'i', 'j', 'k'])
  
# count number of rows in given dataframe 
# by finding the length of indices
num_rows = len(details.index)
  
print('Number of Rows in given dataframe : ',
      num_rows)

输出:

Number of Rows in given dataframe :  10

3) 使用 Dataframe.apply() 计算满足条件的 Pandas Dataframe 中的行数。

Dataframe.apply() ,将函数应用于数据帧的所有行以找出行的元素是否满足条件,根据结果返回一个布尔系列。

代码:

# import pandas library as pd
import pandas as pd
  
# List of Tuples
students = [('Ankit', 22, 'Up', 'Geu'),
           ('Ankita', 31, 'Delhi', 'Gehu'),
           ('Rahul', 16, 'Tokyo', 'Abes'),
           ('Simran', 41, 'Delhi', 'Gehu'),
           ('Shaurya', 33, 'Delhi', 'Geu'),
           ('Harshita', 35, 'Mumbai', 'Bhu' ),
           ('Swapnil', 35, 'Mp', 'Geu'),
           ('Priya', 35, 'Uk', 'Geu'),
           ('Jeet', 35, 'Guj', 'Gehu'),
           ('Ananya', 35, 'Up', 'Bhu')
            ]
  
# Create a DataFrame object from
# list of tuples with columns
# and indices.
details = pd.DataFrame(students, columns =['Name', 'Age',
                                           'Place', 'College'],
                        index =['a', 'b', 'c', 'd', 'e', 
                                'f', 'g', 'i', 'j', 'k'])
  
# Get a bool series representing which row
# satisfies the condition i.e. True for
# row in which 'College' is 'Geu'
details = details.apply(lambda x : True
            if x['College'] == "Geu" else False, axis = 1)
  
# Count number of True in the series
num_rows = len(details[details == True].index)
  
print('Number of Rows in dataframe in which College is Geu : ',
      num_rows )

输出:

Number of Rows in dataframe in which College is Geu :  4