How to Access or Index a Single Element of a DataFrame from a CSV File in Python

Posted on

Introduction

This is a specific article where the main purpose is to be able to get or to retrieve a single element by accessing or indexing the DataFrame object. In this context, the initialization of the DataFrame object is using a CSV file. Moreover, the CSV file exist and retrieved as an example in this link. After initializing the DataFrame object, just perform specific task using it. So, the following is the preparation using that CSV file. But before going ahead with that step, just make sure to do the following step :

  1. In the first step, just make sure that Python tool exist in the local device. For a reference, just check ‘How to Install Python in Microsoft Windows‘ and ‘How to Install Python in Microsoft Windows 11‘ for further information on installing Python.

  2. Next, just make sure that the Pandas library is available in the local device. Just read ‘How to Install Pandas‘ in order to make Pandas library is available as a reference.

  3. Moreover, in order to use Pandas library, just read ‘How to Use Pandas‘ as an additional information.

How to Access or Index a Single Element of a DataFrame from a CSV File in Python

As the preparation is complete, start to begin the process for accessing or indexing just a single element from a DataFrame. But in this context, the DataFrame content will be retrieved from a CSV fie. Below are all of the steps in complete order :

  1. First of all, the example will be several command execution in the command line. So, as the example is using local device using Microsoft Windows operating system, just execute Command Prompt. Below is the presentation of the Command Prompt itself :

    Microsoft Windows [Version 10.0.22000.978]
    (c) Microsoft Corporation. All rights reserved.
    C:\Users\Personal>
  2. Following after, just execute ‘python’ tool by typing ‘python’ in the Command Prompt :

    Microsoft Windows [Version 10.0.22000.978]
    (c) Microsoft Corporation. All rights reserved.
    C:\Users\Personal>python
    Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
  3. Continue on the process, just import the Pandas library in order to start using DataFrame as follows :

    Microsoft Windows [Version 10.0.22000.978]
    (c) Microsoft Corporation. All rights reserved.
    C:\Users\Personal>python
    Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import pandas as pd
    >>>
  4. After successfully importing Pandas ilbrary, start defining the DataFrame by reading the CSV file as follows :

    Microsoft Windows [Version 10.0.22000.978]
    (c) Microsoft Corporation. All rights reserved.
    C:\Users\Personal>python
    Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import pandas as pd
    

    Below is the continuation of the process for reading the CSV file where as an example, the CSV file can be retrieved from this link :

    >>> df_nba = pd.read_csv('nba.csv')

    Furthermore, execute the describe() and also info() method to get the necessary information about the DataFrame as follows :

    >>> df_nba.describe()
              Number        Age     Weight       Salary
    count 457.000000 457.000000 457.000000 4.460000e+02
    mean   17.678337  26.938731 221.522976 4.842684e+06
    std    15.966090   4.404016  26.368343 5.229238e+06
    min     0.000000  19.000000 161.000000 3.088800e+04
    25%     5.000000  24.000000 200.000000 1.044792e+06
    50%    13.000000  26.000000 220.000000 2.839073e+06
    75%    25.000000  30.000000 240.000000 6.500000e+06
    max    99.000000  40.000000 307.000000 2.500000e+07
    >>> df_nba.info()
    <class 'pandas.core.frame.DataFrame'>
    RangeIndex: 458 entries, 0 to 457
    Data columns (total 9 columns):
    #   Column   Non-Null Count Dtype
    --- ------   -------------- -----
    0   Name     457 non-null   object
    1   Team     457 non-null   object
    2   Number   457 non-null   float64
    3   Position 457 non-null   object
    4   Age      457 non-null   float64
    5   Height   457 non-null   object
    6   Weight   457 non-null   float64
    7   College  373 non-null   object
    8   Salary   446 non-null   float64
    dtypes: float64(4), object(5)
    memory usage: 32.3+ KB
    >>>
    
  5. Last but not least, after retrieving the necessary information just perform for accessing or indexing the element. In this context, in order to get single element is only possible for DataFrame to point out using the column name as a reference. So, the following is a simple operating for accessing or indexing the DataFrame using the column name. As a result, it will retrieve all the element only on that column which as an example is choosing the ‘Name’ column :

    >>> print(df_nba['Name']);
    0 Avery Bradley
    1 Jae Crowder
    2 John Holland
    3 R.J. Hunter
    4 Jonas Jerebko
    ...
    453 Shelvin Mack
    454 Raul Neto
    455 Tibor Pleiss
    456 Jeff Withey
    457 NaN
    Name: Name, Length: 458, dtype: object
    >>>

    As for retrieving one full record or row, it cannot use a simple access or indexing syntax pattern with a bracket. The bracket format only accept value in the form of the string key value which is representing the column name of the DataFrame, such as ‘Name’ as in the above example.

Leave a Reply