How do you programmatically retrieve the number of columns in a pandas data frame? I was hoping for something like:
df.num_columns
Same:
import pandas as pd df = pd.DataFrame({"pear": [1,2,3], "apple": [2,3,4], "orange": [3,4,5]}) len(df.columns) 3
Alternative:
df.shape[1]
( df.shape[0] - number of lines)
df.shape[0]
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
This worked for me Len (list (DF)).