Python pandas dataframe: get number of columns

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

df.num_columns 
+112
python pandas dataframe
Nov 30 '13 at 6:28
source share
4 answers

Same:

 import pandas as pd df = pd.DataFrame({"pear": [1,2,3], "apple": [2,3,4], "orange": [3,4,5]}) len(df.columns) 3 
+193
Nov 30 '13 at 7:11
source share

Alternative:

 df.shape[1] 

( df.shape[0] - number of lines)

+67
Nov 30 '13 at 18:56
source share

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

 len(df.columns) 

sets the number of columns.

And for those who want the number of rows:

 len(df.index) 

For a tuple containing the number of rows and columns:

 df.shape 
+17
Jan 26 '15 at 12:50
source share

This worked for me Len (list (DF)).

0
Jan 29 '19 at 7:19
source share



All Articles