Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I retrieve the number of columns in a Pandas data frame?

People also ask

How do I get a list of columns in a data frame?

To get a list of columns from the DataFrame header use DataFrame. columns. values. tolist() method.

How do I count rows and columns in Pandas?

To get the number of rows, and columns we can use len(df. axes[]) function in Python.

How can you get the number of rows and columns present in a data frame?

len() method is used to get the number of rows and number of columns individually.


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)


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

Surprised I haven't seen this yet, so without further ado, here is:

df.columns.size


df.info() function will give you result something like as below. If you are using read_csv method of Pandas without sep parameter or sep with ",".

raw_data = pd.read_csv("a1:\aa2/aaa3/data.csv")
raw_data.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5144 entries, 0 to 5143
Columns: 145 entries, R_fighter to R_age

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).

  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

  2. len(local_df.columns) --> columns attribute will return index object of data frame columns & len function will return total available columns.

  3. 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)