Python 如何检索 Pandas 数据框中的列数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20297332/
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 20:07:45  来源:igfitidea点击:

How do I retrieve the number of columns in a Pandas data frame?

pythonpandasdataframe

提问by user1802143

How do you programmatically retrieve the number of columns in a pandas dataframe? I was hoping for something like:

如何以编程方式检索 Pandas 数据框中的列数?我希望是这样的:

df.num_columns

采纳答案by John

Like so:

像这样:

import pandas as pd
df = pd.DataFrame({"pear": [1,2,3], "apple": [2,3,4], "orange": [3,4,5]})

len(df.columns)
3

回答by mkln

Alternative:

选择:

df.shape[1]

(df.shape[0]is the number of rows)

df.shape[0]是行数)

回答by multigoodverse

If the variable holding the dataframe is called df, then:

如果保存数据帧的变量称为 df,则:

len(df.columns)

gives the number of columns.

给出列数。

And for those who want the number of rows:

对于那些想要行数的人:

len(df.index)

For a tuple containing the number of both rows and columns:

对于包含行数和列数的元组:

df.shape

回答by Tanmay Ghanekar

This worked for me len(list(df)).

这对我有用 len(list(df))。

回答by AshishSingh007

df.info() function will give you result something like as below. If you are using read_csv method of Pandas without sep parameter or sep with ",".

df.info() 函数会给你如下结果。如果您使用的是不带 sep 参数或带“,”的 sep 的 Pandas 的 read_csv 方法。

raw_data = pd.read_csv("a1:\aa2/aaa3/data.csv")
raw_data.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5144 entries, 0 to 5143
Columns: 145 entries, R_fighter to R_age

回答by AshishSingh007

There are multiple option to get column number and column information such as:
let's check them.

有多种选项可以获取列号和列信息,例如:
让我们检查一下。

local_df = pd.DataFrame(np.random.randint(1,12,size=(2,6)),columns =['a','b','c','d','e','f']) 1. local_df.shape[1] --> Shape attribute return tuple as (row & columns) (0,1).

local_df = pd.DataFrame(np.random.randint(1,12,size=(2,6)),columns =['a','b','c','d','e','f ']) 1. local_df.shape[1] --> Shape 属性返回元组为(行和列)(0,1)。

  1. local_df.info() --> info Method will return detailed information about data frame and it's columns such column count, data type of columns, Not null value count, memory usage by Data Frame

  2. len(local_df.columns) --> columns attribute will return index object of data frame columns & len function will return total available columns.

  3. local_df.head(0) --> head method with parameter 0 will return 1st row of df which actually nothing but header.

  1. local_df.info() --> info 方法将返回有关数据框及其列的详细信息,例如列数、列的数据类型、非空值计数、数据框的内存使用情况

  2. len(local_df.columns) --> columns 属性将返回数据框列的索引对象,len 函数将返回可用列的总数。

  3. local_df.head(0) --> 参数为 0 的 head 方法将返回 df 的第一行,它实际上只是标题。

Assuming number of columns are not more than 10. For loop fun: li_count =0 for x in local_df: li_count =li_count + 1 print(li_count)

假设列数不超过 10。for 循环乐趣:li_count =0 for x in local_df: li_count =li_count + 1 print(li_count)