📜  .size pandas - Python (1)

📅  最后修改于: 2023-12-03 14:59:01.751000             🧑  作者: Mango

.size in Pandas - Python

In this tutorial, we will explore the .size attribute in the Pandas library for Python. The .size attribute allows us to find the number of elements in a DataFrame or Series.

Syntax

The basic syntax to use the .size attribute is as follows:

DataFrame.size
Series.size
Parameters

The .size attribute doesn't accept any parameters.

Returns

The .size attribute returns the total number of elements in a DataFrame or Series.

Example: Using .size in Pandas

Let's consider a few examples to understand the usage of the .size attribute:

Example 1: Using .size on a DataFrame
import pandas as pd

data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'],
        'Age': [25, 32, 28, 45],
        'Salary': [50000, 75000, 60000, 90000]}

df = pd.DataFrame(data)

print("Size of DataFrame:", df.size)

Output:

Size of DataFrame: 12

In this example, we create a DataFrame df with 4 rows and 3 columns. The .size attribute returns the total number of elements in the DataFrame, which is 12 in this case.

Example 2: Using .size on a Series
import pandas as pd

s = pd.Series([4, 3, 7, 2, 6, 1])

print("Size of Series:", s.size)

Output:

Size of Series: 6

In this example, we create a Series s with 6 elements. The .size attribute returns the total number of elements in the Series, which is 6 in this case.


That's all about the .size attribute in Pandas. It is a useful attribute to quickly determine the number of elements in a DataFrame or Series. Use it to efficiently compute the size of your data structures in Pandas!

For more detailed information, you can refer to the Pandas documentation on .size