QBoard » Artificial Intelligence & ML » AI and ML - Python » How do I read a specific column in a CSV file in pandas?

How do I read a specific column in a CSV file in pandas?

  • How do I read a specific column in a CSV file in pandas?
      August 7, 2021 7:07 PM IST
    0
  • To read a specific column in a CSV file in pandas, you can use the pandas.read_csv function and specify the desired column(s) in the usecols parameter.

    Read CSV file and include columns "B" and "C"

    df = pd.read_csv("file.csv", usecols=["B", "C"])

    This will read the specified columns and create a Pandas DataFrame with those columns. You can then perform operations on the DataFrame as needed.
      January 30, 2023 12:20 PM IST
    0
  • Let us see how to read specific columns of a CSV file using Pandas. This can be done with the help of the pandas.read_csv() method. We will pass the first parameter as the CSV file and the second parameter the list of specific columns in the keyword usecols. It will return the data of the CSV file of specific columns.

      January 3, 2022 1:58 PM IST
    0
  • An easy way to do this is using the pandas library like this.

    import pandas as pd
    fields = ['star_name', 'ra']
    
    df = pd.read_csv('data.csv', skipinitialspace=True, usecols=fields)
    # See the keys
    print df.keys()
    # See content in 'star_name'
    print df.star_name​


    The problem here was the skipinitialspace which remove the spaces in the header. So ' star_name' becomes 'star_name'

      August 9, 2021 1:27 PM IST
    0
  • To read a CSV file, call pd.read_csv(file_name, usecols=cols_list) with file_name as the name of the CSV file, delimiter as the delimiter, and cols_list as the list of specific columns to read from the CSV file. Call df[col] with df as the DataFrame from the previous step, and col as the column name to read.

    SAMPLE_FILE.CSV
    ,Name,Department
    0,John,IT
    1,Jane,HR
    2,Jack,Accounting
    col_list = ["Name", "Department"]
    df = pd.read_csv("sample_file.csv", usecols=col_list)
    
    print(df["Name"])
    OUTPUT
    0    John
    1    Jane
    2    Jack
    Name: Name, dtype: object
    print(df["Department"])
    OUTPUT
    0            IT
    1            HR
    2    Accounting
    Name: Department, dtype: object​
     
    This post was edited by Vaibhav Mali at January 7, 2022 12:41 PM IST
      January 7, 2022 12:41 PM IST
    0
  • Call pandas.read_csv(filepath_or_buffer, skipinitialspace=True, usecols=columns) with the name of a .csv file as filepath_or_buffer and a list of the desired columns' names as columns
      August 11, 2021 5:25 PM IST
    0