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

后端 未结 9 2112
心在旅途
心在旅途 2021-01-29 18:06

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

df.num_columns
9条回答
  •  鱼传尺愫
    2021-01-29 19:09

    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).

    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.

    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)

提交回复
热议问题