Python|熊猫 dataframe.all()
Python是一种用于进行数据分析的出色语言,主要是因为以数据为中心的Python包的奇妙生态系统。 Pandas就是其中之一,它使导入和分析数据变得更加容易。
DataFrame.all()
方法检查所有元素是否为真,可能在一个轴上。如果系列中或沿 Dataframe 轴的所有元素均非零、非空或非 False,则返回 True。
Syntax: DataFrame.all(axis=0, bool_only=None, skipna=True, level=None, **kwargs)
Parameters:
axis : {0 or ‘index’, 1 or ‘columns’, None}, default 0
Indicate which axis or axes should be reduced.
0 / ‘index’ : reduce the index, return a Series whose index is the original column labels.
1 / ‘columns’ : reduce the columns, return a Series whose index is the original index.
None : reduce all axes, return a scalar.
skipna : Exclude NA/null values. If an entire row/column is NA, the result will be NA.
level : If the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a Series.
bool_only : Include only boolean columns. If None, will attempt to use everything, then use only boolean data. Not implemented for Series.
**kwargs : Additional keywords have no effect but might be accepted for compatibility with NumPy.
Returns: all : Series or DataFrame (if level specified)
注意: Nan值将被视为非空值,因此将被评估为 True。
有关代码中使用的 CSV 文件的链接,请单击此处
示例 #1:在数据框中的每一列中添加后缀_col
。
# importing pandas as pd
import pandas as pd
# Making data frame from the csv file
df = pd.read_csv("nba.csv")
# Printing the first 10 rows of the
# data frame for visualization
df[:10]
# checking for 'Name' column
df.Name.all()
输出:
示例 #2:评估列行为
dataframe.all()
默认行为检查列值是否都返回 True。
# Checking for all the columns in the dataframe
df.all()
输出:
示例 #3:检查逐行元素
指定 axis='columns' 以检查逐行值是否都返回 True。如果任何特定行中的所有值都评估为真,那么整个行将被评估为真。
# importing pandas as pd
import pandas as pd
# Making data frame from the csv file
df = pd.read_csv("nba.csv")
# Checking across the row
df.all(axis ='columns')
输出:
all()
计算数据帧中所有行的所有值,并为每一行输出一个布尔值。
示例 #4:检查数据框中的所有值
指定,axis=None 是否每个值在数据框中都为 True。
# importing pandas as pd
import pandas as pd
# Making data frame from the csv file
df = pd.read_csv("nba.csv")
# Checking across the row
df.all(axis = None)
输出: