To get the number of columns in a pandas dataframe you can use the shape
attribute and select the number of columns by accessing the second value of the resulting tuple. Here's an example:
main.py226 chars13 lines
In this example, df.shape
returns a tuple with two values: the number of rows and the number of columns. Since we want the number of columns, we access the second value of the tuple by indexing with [1]
.
gistlibby LogSnag