Python - 数据框的维度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13921647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 09:56:26  来源:igfitidea点击:

Python - Dimension of Data Frame

pythonpandas

提问by user1911092

New to Python.

Python 新手。

In R, you can get the dimension of a matrix using dim(...). What is the corresponding function in Python Pandas for their data frame?

在 R 中,您可以使用 dim(...) 获取矩阵的维度。Python Pandas 的数据框对应的函数是什么?

采纳答案by BrenBarn

df.shape, where dfis your DataFrame.

df.shapedf你的 DataFrame在哪里。

回答by Ted Petrou

Summary of all ways to get info on dimensions of DataFrame or Series

获取有关 DataFrame 或 Series 维度信息的所有方法的摘要

There are a number of ways to get information on the attributes of your DataFrame or Series.

有多种方法可以获取有关 DataFrame 或 Series 属性的信息。

Create Sample DataFrame and Series

创建示例数据帧和系列

df = pd.DataFrame({'a':[5, 2, np.nan], 'b':[ 9, 2, 4]})
df

     a  b
0  5.0  9
1  2.0  2
2  NaN  4

s = df['a']
s

0    5.0
1    2.0
2    NaN
Name: a, dtype: float64

shapeAttribute

shape属性

The shapeattribute returns a two-item tuple of the number of rows and the number of columns in the DataFrame. For a Series, it returns a one-item tuple.

shape属性返回 DataFrame 中行数和列数的二项元组。对于系列,它返回一个单项元组。

df.shape
(3, 2)

s.shape
(3,)


lenfunction

len功能

To get the number of rows of a DataFrame or get the length of a Series, use the lenfunction. An integer will be returned.

要获取 DataFrame 的行数或获取系列的长度,请使用该len函数。将返回一个整数。

len(df)
3

len(s)
3


sizeattribute

size属性

To get the total number of elements in the DataFrame or Series, use the sizeattribute. For DataFrames, this is the product of the number of rows and the number of columns. For a Series, this will be equivalent to the lenfunction:

要获取 DataFrame 或 Series 中元素的总数,请使用该size属性。对于 DataFrames,这是行数和列数的乘积。对于系列,这将等效于以下len功能:

df.size
6

s.size
3


ndimattribute

ndim属性

The ndimattribute returns the number of dimensions of your DataFrame or Series. It will always be 2 for DataFrames and 1 for Series:

ndim属性返回 DataFrame 或 Series 的维数。DataFrames 总是 2,Series 总是 1:

df.ndim
2

s.ndim
1


The tricky countmethod

棘手的count方法

The countmethod can be used to return the number of non-missing values for each column/row of the DataFrame. This can be very confusing, because most people normally think of count as just the length of each row, which it is not. When called on a DataFrame, a Series is returned with the column names in the index and the number of non-missing values as the values.

count方法可用于返回 DataFrame 的每一列/行的非缺失值的数量。这可能非常令人困惑,因为大多数人通常认为计数只是每行的长度,但事实并非如此。在 DataFrame 上调用时,将返回一个系列,其中包含索引中的列名和非缺失值的数量作为值。

df.count() # by default, get the count of each column

a    2
b    3
dtype: int64


df.count(axis='columns') # change direction to get count of each row

0    2
1    2
2    1
dtype: int64

For a Series, there is only one axis for computation and so it just returns a scalar:

对于系列,只有一个轴用于计算,因此它只返回一个标量:

s.count()
2


Use the infomethod for retrieving metadata

使用info检索元数据的方法

The infomethod returns the number of non-missing values and data types of each column

info方法返回每列的非缺失值数和数据类型

df.info()

df.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 2 columns):
a    2 non-null float64
b    3 non-null int64
dtypes: float64(1), int64(1)
memory usage: 128.0 bytes