QBoard » Artificial Intelligence & ML » AI and ML - Python » Pandas Column names to list - correct method

Pandas Column names to list - correct method

  • I cannot find any resources about wether one of the following three methods for getting a list of column names is preferred over the others. The first and simplest, seems to work with my current example. Is there any reason I should not use it ?
    >>> import pandas as pd >>> import numpy as np >>> df = pd.DataFrame(np.random.rand(5,3)) >>> df.columns RangeIndex(start=0, stop=3, step=1) >>> list(df.columns) [0, 1, 2] >>> df.columns.get_values().tolist() [0, 1, 2] >>> list(df.columns.get_values()) [0, 1, 2]
    Update
    Performance - related answer here: https://stackoverflow.com/a/27236748/605328
      November 2, 2021 7:44 PM IST
    0
  • You can also use:

    df.columns.tolist()
    
      November 3, 2021 2:00 PM IST
    0
  • # Import pandas package
    import pandas as pd
    	
    # making data frame
    data = pd.read_csv("nba.csv")
    
    # iterating the columns
    for col in data.columns:
    	print(col)
    
      November 8, 2021 2:51 PM IST
    0
  • You can also use:
    df.columns.tolist()
      November 8, 2021 5:23 PM IST
    0
  • Let us first load Pandas.

    # load pandas
    import pandas as pd
    

    And we will use College tuition data from tidytuesday project illustrate extracting column names as a list. Let us load the dataset directly from tidytuesday project’s github page.

    data_url="https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-03-10/tuition_cost.csv"
    df = pd.read_csv(data_url)
    df.iloc[0:3,0:5]
     
            name    state   state_code  type    degree_length
    0   Aaniiih Nakoda College  Montana MT  Public  2 Year
    1   Abilene Christian University    Texas   TX  Private 4 Year
    2   Abraham Baldwin Agricultural College    Georgia GA  Public  2 Year

     

     

    We can get the names of columns of Pandas dataframe using Pandas method “columns”.

     
    # Extract Column Names of a Pandas Dataframe
    df.columns


    Pandas’ columns method returns the names as Pandas Index object.

    Index(['name', 'state', 'state_code', 'type', 'degree_length',
           'room_and_board', 'in_state_tuition', 'in_state_total',
           'out_of_state_tuition', 'out_of_state_total'],
          dtype='object')

    We can convert the Pandas Index object to list using the tolist() method.

     
    # Extract Column Names as List in Pandas Dataframe
    df.columns.tolist()

    And now we have Pandas’ dataframe column names as a list.

    ['name',
     'state',
     'state_code',
     'type',
     'degree_length',
     'room_and_board',
     'in_state_tuition',
     'in_state_total',
     'out_of_state_tuition',
     'out_of_state_total']

    Another way to get column names as list is to first convert the Pandas Index object as NumPy Array using the method “values” and convert to list as shown below.

    df.columns.values.tolist()

    And we would get the Pandas column names as a list.

    ['name',
     'state',
     'state_code',
     'type',
     'degree_length',
     'room_and_board',
     'in_state_tuition',
     'in_state_total',
     'out_of_state_tuition',
     'out_of_state_total']

    This post is part of the series on Pandas 101, a tutorial covering tips and tricks on using Pandas for data munging and analysis.

    This post was edited by Maryam Bains at November 9, 2021 2:21 PM IST
      November 9, 2021 2:18 PM IST
    0