计算Pandas数据框架的行和列的数量
Pandas允许我们通过计算Dataframe中的行和列的数量来获得Dataframe的形状。你可以尝试各种方法来了解如何计算Pandas中的行和列的数量。
示例:
输入:
{'name': ['Katherine', 'James', 'Emily', 'Michael', 'Matthew', 'Laura'],
'score': [98, 80, 60, 85, 49, 92],
'age': [20, 25, 22, 24, 21, 20],
'qualify_label': ['yes', 'yes', 'no','yes', 'no', 'yes']}
输出:
Number of Rows: 6
Number of Columns: 4
使用len(df.axes[])函数计算Dataframe的行和列的数量
让我们举个例子,一个由学生考试成绩数据组成的数据框架。为了得到行和列的数量,我们可以使用Python中的len( df.axes[])函数。
# importing pandas
import pandas as pd
result_data = {'name': ['Katherine', 'James', 'Emily',
'Michael', 'Matthew', 'Laura'],
'score': [98, 80, 60, 85, 49, 92],
'age': [20, 25, 22, 24, 21, 20],
'qualify_label': ['yes', 'yes', 'no',
'yes', 'no', 'yes']}
# creating dataframe
df = pd.DataFrame(result_data, index=None)
# computing number of rows
rows = len(df.axes[0])
# computing number of columns
cols = len(df.axes[1])
print(df)
print("Number of Rows: ", rows)
print("Number of Columns: ", cols)
输出 :

使用info()函数计算Dataframe的行数和列数
Pandas dataframe.info()函数被用来获取Dataframe的简明摘要。在这里我们可以看到,我们得到了一个包含行数和列数的Dataframe的摘要细节。
# importing pandas
import pandas as pd
# creating dataframe
df = pd.DataFrame({'name': ['Katherine', 'James', 'Emily',
'Michael', 'Matthew', 'Laura'],
'score': [98, 80, 60, 85, 49, 92],
'age': [20, 25, 22, 24, 21, 20],
'qualify_label': ['yes', 'yes', 'no',
'yes', 'no', 'yes']})
print(df.info())
输出:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: **6 entries** , **0 to 5**
Data columns ( **total 4 columns** ):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 name 6 non-null object
1 score 6 non-null int64
2 age 6 non-null int64
3 qualify_label 6 non-null object
dtypes: int64(2), object(2)
memory usage: 320.0+ bytes
None
使用len()函数计算Dataframe的行和列的数量
len()函数返回Dataframe的长度行,我们可以使用df.columns过滤一些列,得到列的数量。
# importing pandas
import pandas as pd
# creating dataframe
df = pd.DataFrame({'name': ['Katherine', 'James', 'Emily',
'Michael', 'Matthew', 'Laura'],
'score': [98, 80, 60, 85, 49, 92],
'age': [20, 25, 22, 24, 21, 20],
'qualify_label': ['yes', 'yes', 'no',
'yes', 'no', 'yes']})
print(len(df))
print(len(df.columns))
输出:
6
4
使用shape计算Dataframe的行数和列数
这里,我们将尝试一种不同的方法来计算导入的CSV文件的Dataframe的行和列,并使用df.shape来计算行和列。
# importing pandas
import pandas as pd
# importing csv file
df = pd.read_csv(
'https://raw.githubusercontent.com/uiuc-cse/data-fa14/gh-pages/data/iris.csv')
print(df.head())
# obtaining the shape
print("shape of dataframe", df.shape)
# obtaining the number of rows
print("number of rows : ", df.shape[0])
# obtaining the number of columns
print("number of columns : ", df.shape[1])
输出 :


使用大小来计算数据框架的行和列的数量
大小返回多行和多列,即这里的行数是6,列数是4,所以多行和多列将是6*4=24。
# importing pandas
import pandas as pd
# creating dataframe
df = pd.DataFrame({'name': ['Katherine', 'James', 'Emily',
'Michael', 'Matthew', 'Laura'],
'score': [98, 80, 60, 85, 49, 92],
'age': [20, 25, 22, 24, 21, 20],
'qualify_label': ['yes', 'yes', 'no',
'yes', 'no', 'yes']})
print(df.size)
输出:
24
使用count()和index.计算Pandas数据框架的行数
使用count()和index,我们可以得到数据框架中存在的行数。
# importing pandas
import pandas as pd
# creating dataframe
df = pd.DataFrame({'name': ['Katherine', 'James', 'Emily',
'Michael', 'Matthew', 'Laura'],
'score': [98, 80, 60, 85, 49, 92],
'age': [20, 25, 22, 24, 21, 20],
'qualify_label': ['yes', 'yes', 'no',
'yes', 'no', 'yes']})
print(df[df.columns[0]].count())
print(len(df.index))
输出:
6
6
极客教程