How do I retrieve the number of columns in a Pandas data frame?
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
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
Alternative:
df.shape[1]
(df.shape[0]
is the number of rows)