pythonpandasindex-error

How do I specify Index Values in Python? IndexError: single positional indexer is out-of-bounds


I am able to load this excel worksheet properly but do not know how to specify that I want data from the entire first column to be read. Screenshot of Code

import pandas as pd

# Load Excel sheet for CAP settings
df = pd.read_excel(r'C:\FILENAMEHERE.xlsx')
print (df)

#Find settings on CAP database excel sheet using CAP ID
capacitor_row = capacitor_settings_df[capacitor_settings_df['CAP_ID'] == cap_id_input.iloc[0]

I added a line to print the excel workbook in order to verify that it was loading correctly and it is. I am not sure how to read the first column of the excel sheet which is titled "CAP_ID". I have tried specifying the iloc[1] but do not know how to format this in order to read only the first column.


Solution

  • If CAP_ID is a column in the Excel file, then you can do the following;

    import pandas as pd
    
    # Load Excel sheet for CAP settings
    df = pd.read_excel(r'C:\FILENAMEHERE.xlsx')
    print (df)
    
    #Find settings on CAP database excel sheet using CAP ID
    df['CAP_ID']
    

    If you want to locate values from a column named capacitor_settings based on a known CAP_ID, then you can do the following.

    import pandas as pd 
    
    # Load Excel sheet for CAP settings
    df = pd.read_excel(r'C:\FILENAMEHERE.xlsx')
    print (df)
    
    # select values from a column based on values in another column
    df['capacitor_settings'].where(df['CAP_ID'] == 'xyz')
    

    If you want to locate values from a column named capacitor_settings based on several known values of CAP_ID, then you can do the following.

       # Load Excel sheet for CAP settings
        df = pd.read_excel(r'C:\FILENAMEHERE.xlsx')
        print (df)
    
        # select values from a column based on multipe values in another column
        df['capacitor_settings'].where(df['CAP_ID'].isin(['xyz', 'abc']))