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

后端 未结 9 2116
心在旅途
心在旅途 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:10

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

    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
    

提交回复
热议问题