Подсчитайте все строки или те, которые удовлетворяют некоторому условию в кадре данных Pandas
Давайте посмотрим, как подсчитать количество всех строк в Dataframe или строк, которые удовлетворяют условию в Pandas.
1) Подсчитайте все строки в кадре данных Pandas, используя Dataframe.shape.
Dataframe.shape returns tuple of shape (Rows, columns) of dataframe/series.
Let’s create a pandas dataframe.
# import pandas library as pdimport pandas as pd # List of Tuplesstudents = [("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 |
Выход:

Code: Count all rows
# import pandas library as pdimport pandas as pd # List of Tuplesstudents = [("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 dataframenum_rows = details.shape[0] print("Number of Rows in given dataframe : ", num_rows) |
Выход:
Количество строк в данном фрейме данных: 10
2) Подсчитайте все строки в кадре данных Pandas, используя Dataframe.index.
Dataframe.index attribute gives a sequence of index or row labels.
Code:
import pandas as pd # List of Tuplesstudents = [("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 indicesnum_rows = len(details.index) print("Number of Rows in given dataframe : ", num_rows) |
Выход:
Количество строк в данном фрейме данных: 10
3) Подсчитайте строки в кадре данных Pandas, который удовлетворяет условию, используя Dataframe.apply ().
Dataframe.apply(), apply function to all the rows of a dataframe to find out if elements of rows satisfies a condition or not, Based on the result it returns a bool series.
Code:
# import pandas library as pdimport pandas as pd # List of Tuplesstudents = [("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 seriesnum_rows = len(details[details == True].index) print("Number of Rows in dataframe in which College is Geu : ", num_rows ) |
Выход:
Количество строк в фреймворке, в котором College является Geu: 4
Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.
Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.