# 計算 Pandas DataFrame 列的數量

Samreena Aslam 2023年1月30日 2022年5月16日

## 使用 `column` 屬性計算 Pandas `DataFrame` 的列數

``````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` 的列數

``````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])
``````

## 使用型別轉換計算 Pandas `DataFrame` 的列數

``````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` 的列數

``````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()

``````