python获取dataframe的列数
时间: 2023-10-16 07:06:01 浏览: 93
python DataFrame获取行数、列数、索引及第几行第几列的值方法
要获取DataFrame的列数,有几种方法可以实现。
方法1: 使用columns属性
import pandas as pd
data = {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}
df = pd.DataFrame(data)
num_columns = len(df.columns)
print("列数:", num_columns) [1]
方法2: 使用shape属性
import pandas as pd
data = {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}
df = pd.DataFrame(data)
num_columns = df.shape<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [【python】【pandas】获得DataFrame的列数的两种方法:shape属性或columns属性。](https://blog.csdn.net/Good_Hope/article/details/130995667)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
- *3* [python DataFrame获取行数、列数、索引及第几行第几列的值方法](https://download.csdn.net/download/weixin_38722348/14861411)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文