How do you programmatically retrieve the number of columns in a pandas dataframe? I was hoping for something like:
df.num_columns
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).
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
len(local_df.columns) --> columns attribute will return index object of data frame columns & len function will return total available columns.
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)