计算 Pandas DataFrame 列的数量
在 Pandas DataFrame
中,数据以表格格式存储或显示,如 rows
和 columns
。Pandas 通过使用各种方法帮助我们检索或计算 DataFrame
中的行数和列数。
我们将在本教程中探索与计算 Pandas DataFrame
的列数相关的各种方法。
使用 column
属性计算 Pandas DataFrame
的列数
使用 Pandas DataFrame
的 column
属性,我们可以检索列列表并计算列长度并计算 DataFrame
中的列数。
请参阅以下示例。首先,我们创建了一个产品的 DataFrame
。使用 column_list = dataframe.columns
,我们检索列列表,然后使用 len(column_list)
计算列数。
示例代码:
import pandas as pd
import numpy as np
from IPython.display import display
# creating a DataFrame
dict = {
"Products": ["Intel Dell Laptops", "HP Laptops", "Lenavo Laptops", "Acer Laptops"],
"Price dollar": [350, 300, 400, 250],
"Percentage Sale": [83, 99, 84, 76],
}
dataframe = pd.DataFrame(dict)
# displaying the DataFrame
display(dataframe)
# To get the list of columns of dataframe
column_list = dataframe.columns
# Printing Number of columns
print("Number of columns:", len(column_list))
输出:
使用 shape
属性计算 Pandas DataFrame
的列数
当使用 shape
属性时,它会检索表示 DataFrame
形状的元组。在以下示例中,shape=dataframe.shape
行将返回 DataFrame
形状,而 shape[1]
计算列数。
示例代码:
import pandas as pd
import numpy as np
from IPython.display import display
# creating a DataFrame
dict = {
"Products": ["Intel Dell Laptops", "HP Laptops", "Lenavo Laptops", "Acer Laptops"],
"Price dollar": [350, 300, 400, 250],
"Percentage Sale": [83, 99, 84, 76],
"quantity": [10, 16, 90, 100],
}
dataframe = pd.DataFrame(dict)
# displaying the DataFrame
display(dataframe)
# Get shape of the dataframe
shape = dataframe.shape
# Printing Number of columns
print("Number of columns :", shape[1])
输出:
正如我们在上面的输出中看到的那样,它显示了上面示例中的 4
的总列数
。
使用类型转换计算 Pandas DataFrame
的列数
我们在这个方法中使用了类型转换的方法,它几乎类似于列属性。当我们对 DataFrame
列表使用 typecasting
时,它会检索列名列表。有关类型转换方法的更多理解,请参见以下示例:
示例代码:
import pandas as pd
import numpy as np
from IPython.display import display
# creating a DataFrame
dict = {
"Products": ["Intel Dell Laptops", "HP Laptops", "Lenavo Laptops", "Acer Laptops"],
"Price dollar": [350, 300, 400, 250],
"Percentage Sale": [83, 99, 84, 76],
"quantity": [10, 16, 90, 100],
}
dataframe = pd.DataFrame(dict)
# displaying the DataFrame
display(dataframe)
# Typecasting dataframe to list
dataframe_list = list(dataframe)
# Printing Number of columns
print("Number of columns :", len(dataframe_list))
输出:
使用 dataframe.info()
方法计算 Pandas DataFrame
的列数
使用 info()
方法,我们可以打印 Pandas DataFrame
的完整简明摘要。在以下示例中,我们在源代码末尾使用了 dataframe.info()
。它显示与 DataFrame
类、dtypes
、内存使用情况、列数和范围索引相关的信息。
示例代码:
import pandas as pd
import numpy as np
from IPython.display import display
# creating a DataFrame
dict = {
"Products": ["Intel Dell Laptops", "HP Laptops", "Lenavo Laptops", "Acer Laptops"],
"Price dollar": [350, 300, 400, 250],
"Percentage Sale": [83, 99, 84, 76],
"quantity": [10, 16, 90, 100],
}
dataframe = pd.DataFrame(dict)
# displaying the DataFrame
display(dataframe)
# Print dataframe information using info() method
dataframe.info()
输出:
在上图中,我们可以看到 DataFrame
的简明摘要,包括列数。
相关文章
Pandas DataFrame DataFrame.shift() 函数
发布时间:2024/04/24 浏览次数:133 分类:Python
-
DataFrame.shift() 函数是将 DataFrame 的索引按指定的周期数进行移位。
Python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:Python
-
Python Pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
Pandas read_csv()函数
发布时间:2024/04/24 浏览次数:254 分类:Python
-
Pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 DataFrame 中。
Pandas 多列合并
发布时间:2024/04/24 浏览次数:628 分类:Python
-
本教程介绍了如何在 Pandas 中使用 DataFrame.merge()方法合并两个 DataFrames。
Pandas loc vs iloc
发布时间:2024/04/24 浏览次数:837 分类:Python
-
本教程介绍了如何使用 Python 中的 loc 和 iloc 从 Pandas DataFrame 中过滤数据。
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串