Python Forum
IndexError: list index out of range
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
IndexError: list index out of range
#1
Hi,
Below is the code that worked before, it gives the error "list index out of range".
I don't know where is the wrong. Could you please help me?

Best regards,


import numpy as np
from matplotlib import cbook
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import os.path

import pandas

# fileName = input("This program is designed to draw 3D graphics.\nThe file structure must have Latitude, Longitude, Depth, and Magnitude columns.\nPlease enter the file name (e.g '/path/to/file.name') :")
fileName = 'D:/PYTHON STUDIES/Seismic_Data_Analysis/01011900-30092016_AllData.txt'
name, extension = os.path.splitext(fileName)


def fileExtension(extension):
    return {
        '.txt': 1,
        '.csv': 2,
        '.xls': 3,
        '.xlsx': 4
    }[extension]


def plot_3D_graph(all_data):
    ax = plt.figure(figsize=(10, 5)).gca(projection='3d')

    for i in range(5, len(all_data)):
        if all_data[0:3][i][3] < 3.0:
            color = 'grey'
            marker_size = 1 * all_data[0:3][i][3]
            label = '$0 < M < 3.0$'
        elif 3.0 <= all_data[0:3][i][3] < 4.0:
            color = 'blue'
            marker_size = 2 * all_data[0:3][i][3]
            label = '$3.0 <= M < 4.0$'
        elif 4.0 <= all_data[0:3][i][3] < 5.0:
            color = 'black'
            marker_size = 3 * all_data[0:3][i][3]
            label = '$4.0 <= M < 5.0$'
        elif 5.0 <= all_data[0:3][i][3] < 6.0:
            color = 'limegreen'
            marker_size = 4 * all_data[0:3][i][3]
            label = '$5.0 <= M < 6.0$'
        elif 6.0 <= all_data[0:3][i][3] < 7.0:
            color = 'lightcoral'
            marker_size = 5 * all_data[0:3][i][3]
            label = '$6.0 <= M < 7.0$'
        elif all_data[0:3][i][3] >= 7.0:
            color = 'red'
            marker_size = 7 * all_data[0:3][i][3]
            label = '$M >= 7.0$'

        ax.scatter(all_data[0:3][i][0], all_data[0:3][i][1], all_data[0:3][i][2], marker='o', label=label, color=color, s=marker_size)

    ax.set_xlabel('Latitude')
    ax.set_ylabel('Longitude')
    ax.set_zlabel('Depth(Km)')
    plt.legend(loc='upper left')
    plt.show()


def read_txt_csv_file(fileName):
    all = pandas.read_csv(fileName, usecols=["Latitude", "Longitude", "Depth", "Magnitude"], encoding='latin-1', delimiter=';').values
    all_data = sorted(all, key=lambda x: float(x[3]))  # sorted by Magnitude column ................
    plot_3D_graph(all_data)


def read_xls_file(fileName):
    pass


def read_xlsx_file(fileName):
    pass


if fileExtension(extension) == 1:
    read_txt_csv_file(fileName)
elif fileExtension(extension) == 2:
    read_txt_csv_file(fileName)
elif fileExtension(extension) == 3:
    read_xls_file(fileName)
elif fileExtension(extension) == 4:
    read_xlsx_file(fileName)
else:
    print("Your file is different from '.txt', '.csv', '.xls', '.xlsx' file types.")
Reply
#2
Would you please supply a sample '01011900-30092016_AllData.txt' file.
Reply
#3
(Sep-07-2021, 08:30 AM)Larz60+ Wrote: Would you please supply a sample '01011900-30092016_AllData.txt' file.

Attached Files

.txt   01011900-30092016_AllData.txt (Size: 198.7 KB / Downloads: 46)
Reply
#4
since you only use pandas to read the file, perhaps a better solution would be:
from pathlib import Path
import csv
from CreateDict import CreateDict
import os

class DataPlot:
    def __init__(self):
        # Set starting directory same as code
        os.chdir(os.path.abspath(os.path.dirname(__file__)))
        self.cd = CreateDict()
        homepath = Path('.')
        self.infile = homepath / '01011900-30092016_AllData.txt'
    
    def dispatch(self):
        data = self.read_data()
    
    def read_data(self):
        with self.infile.open() as fp:
            crdr = csv.DictReader(fp, delimiter=';')
            for n, row in enumerate(crdr):
                # Replace print statement with your code for each row
                print(f"\n{n}: {self.cd.display_dict(row)}")


def main():
    dp = DataPlot()
    dp.dispatch()


if __name__ == '__main__':
    main()
This will read each row of the csv file into a dictionary format like:
Output:
AgencyID: Ambraseys-Finkel Time: 18.01.1900 15:30:00 Day: 18 Month: 01 Year: 1900 Hour: 15 Minute: 30 Second: 0 MiliSecond: 0 Latitude: 37.7900 Longitude: 28.2100 Depth: -10.0 Fixed: * Mtype: MS Magnitude: 4.40 M0: Rms: Herr: Verr: Gap: StationNO: Country: TÜRKÝYE City: AYDIN District: Yenipazar Town: Other: EventType: Eq
you will need CreateDict.py:
import os
 
class CreateDict:
    """
    Generic Software tools used by Trailmapper.
 
    CreateDict.py - Contains methods to simplify node and cell creation within
                    a dictionary
 
    Usage: 
     
        The best way to learn what can be done is to examine the testit function
        included in this module.
 
        new_dict(dictname) - Creates a new dictionary instance with the name
            contained in dictname
 
        add_node(parent, nodename) - Creates a new node (nested dictionary)
            named in nodename, in parent dictionary.
 
        add_cell(nodename, cellname, value) - Creates a leaf node within node
            named in nodename, with a cell name of cellname, and value of value.
 
        display_dict(dictname) - Recursively displays a nested dictionary.
 
    Requirements:
 
        Trailmapper software:
            None
 
        Python standard library:
            os
     
    Author: Larz60+  -- May 2019.
    """
    def __init__(self):
        os.chdir(os.path.abspath(os.path.dirname(__file__)))
 
    def new_dict(self, dictname):
        setattr(self, dictname, {})
 
    def add_node(self, parent, nodename):
        node = parent[nodename] = {}
        return node
 
    def add_cell(self, nodename, cellname, value):
        cell =  nodename[cellname] = value
        return cell
 
    def display_dict(self, dictname, level=0):
        indent = " " * (4 * level)
        for key, value in dictname.items():
            if isinstance(value, dict):
                print(f'\n{indent}{key}')
                level += 1
                self.display_dict(value, level)
            else:
                print(f'{indent}{key}: {value}')
            if level > 0:
                level -= 1
 
 
def testit():
    # instantiate class
    cd = CreateDict()
 
    # create new dictionary named CityList
    cd.new_dict('CityList')
 
    # add node Boston
    boston = cd.add_node(cd.CityList, 'Boston')
    # add sub node Resturants
    bos_resturants = cd.add_node(boston, 'Resturants')
 
    # Add subnode 'Spoke Wine Bar' to parent bos_resturants
    spoke = cd.add_node(bos_resturants, 'Spoke Wine Bar')
    cd.add_cell(spoke, 'Addr1', '89 Holland St')
    cd.add_cell(spoke, 'City', 'Sommerville')
    cd.add_cell(spoke, 'Addr1', '02144')
    cd.add_cell(spoke, 'Phone', '617-718-9463')
 
    # Add subnode 'Highland Kitchen' to parent bos_resturants
    highland = cd.add_node(bos_resturants, 'Highland Kitchen')
    cd.add_cell(highland, 'Addr1', '150 Highland Ave')
    cd.add_cell(highland, 'City', 'Sommerville')
    cd.add_cell(highland, 'ZipCode', '02144')
    cd.add_cell(highland, 'Phone', '617-625-1131')
 
    # display dictionary
    print(f'\nCityList Dictionary')
    cd.display_dict(cd.CityList)
    print(f'\nraw data: {cd.CityList}')
 
if __name__ == '__main__':
    testit()
Reply
#5
(Sep-07-2021, 11:25 AM)Larz60+ Wrote: since you only use pandas to read the file, perhaps a better solution would be:
from pathlib import Path
import csv
from CreateDict import CreateDict
import os

class DataPlot:
    def __init__(self):
        # Set starting directory same as code
        os.chdir(os.path.abspath(os.path.dirname(__file__)))
        self.cd = CreateDict()
        homepath = Path('.')
        self.infile = homepath / '01011900-30092016_AllData.txt'
    
    def dispatch(self):
        data = self.read_data()
    
    def read_data(self):
        with self.infile.open() as fp:
            crdr = csv.DictReader(fp, delimiter=';')
            for n, row in enumerate(crdr):
                # Replace print statement with your code for each row
                print(f"\n{n}: {self.cd.display_dict(row)}")


def main():
    dp = DataPlot()
    dp.dispatch()


if __name__ == '__main__':
    main()
This will read each row of the csv file into a dictionary format like:
Output:
AgencyID: Ambraseys-Finkel Time: 18.01.1900 15:30:00 Day: 18 Month: 01 Year: 1900 Hour: 15 Minute: 30 Second: 0 MiliSecond: 0 Latitude: 37.7900 Longitude: 28.2100 Depth: -10.0 Fixed: * Mtype: MS Magnitude: 4.40 M0: Rms: Herr: Verr: Gap: StationNO: Country: TÜRKÝYE City: AYDIN District: Yenipazar Town: Other: EventType: Eq
you will need CreateDict.py:
import os
 
class CreateDict:
    """
    Generic Software tools used by Trailmapper.
 
    CreateDict.py - Contains methods to simplify node and cell creation within
                    a dictionary
 
    Usage: 
     
        The best way to learn what can be done is to examine the testit function
        included in this module.
 
        new_dict(dictname) - Creates a new dictionary instance with the name
            contained in dictname
 
        add_node(parent, nodename) - Creates a new node (nested dictionary)
            named in nodename, in parent dictionary.
 
        add_cell(nodename, cellname, value) - Creates a leaf node within node
            named in nodename, with a cell name of cellname, and value of value.
 
        display_dict(dictname) - Recursively displays a nested dictionary.
 
    Requirements:
 
        Trailmapper software:
            None
 
        Python standard library:
            os
     
    Author: Larz60+  -- May 2019.
    """
    def __init__(self):
        os.chdir(os.path.abspath(os.path.dirname(__file__)))
 
    def new_dict(self, dictname):
        setattr(self, dictname, {})
 
    def add_node(self, parent, nodename):
        node = parent[nodename] = {}
        return node
 
    def add_cell(self, nodename, cellname, value):
        cell =  nodename[cellname] = value
        return cell
 
    def display_dict(self, dictname, level=0):
        indent = " " * (4 * level)
        for key, value in dictname.items():
            if isinstance(value, dict):
                print(f'\n{indent}{key}')
                level += 1
                self.display_dict(value, level)
            else:
                print(f'{indent}{key}: {value}')
            if level > 0:
                level -= 1
 
 
def testit():
    # instantiate class
    cd = CreateDict()
 
    # create new dictionary named CityList
    cd.new_dict('CityList')
 
    # add node Boston
    boston = cd.add_node(cd.CityList, 'Boston')
    # add sub node Resturants
    bos_resturants = cd.add_node(boston, 'Resturants')
 
    # Add subnode 'Spoke Wine Bar' to parent bos_resturants
    spoke = cd.add_node(bos_resturants, 'Spoke Wine Bar')
    cd.add_cell(spoke, 'Addr1', '89 Holland St')
    cd.add_cell(spoke, 'City', 'Sommerville')
    cd.add_cell(spoke, 'Addr1', '02144')
    cd.add_cell(spoke, 'Phone', '617-718-9463')
 
    # Add subnode 'Highland Kitchen' to parent bos_resturants
    highland = cd.add_node(bos_resturants, 'Highland Kitchen')
    cd.add_cell(highland, 'Addr1', '150 Highland Ave')
    cd.add_cell(highland, 'City', 'Sommerville')
    cd.add_cell(highland, 'ZipCode', '02144')
    cd.add_cell(highland, 'Phone', '617-625-1131')
 
    # display dictionary
    print(f'\nCityList Dictionary')
    cd.display_dict(cd.CityList)
    print(f'\nraw data: {cd.CityList}')
 
if __name__ == '__main__':
    testit()

Hi,
Thank you very much for your quick replay.
But, unless I misunderstood your answer, the problem is not related to reading the .csv file. There is a problem with drawing 3D graphics.
Reply
#6
(Sep-07-2021, 12:50 PM)rf_kartal Wrote:
(Sep-07-2021, 11:25 AM)Larz60+ Wrote: since you only use pandas to read the file, perhaps a better solution would be:
from pathlib import Path
import csv
from CreateDict import CreateDict
import os

class DataPlot:
    def __init__(self):
        # Set starting directory same as code
        os.chdir(os.path.abspath(os.path.dirname(__file__)))
        self.cd = CreateDict()
        homepath = Path('.')
        self.infile = homepath / '01011900-30092016_AllData.txt'
    
    def dispatch(self):
        data = self.read_data()
    
    def read_data(self):
        with self.infile.open() as fp:
            crdr = csv.DictReader(fp, delimiter=';')
            for n, row in enumerate(crdr):
                # Replace print statement with your code for each row
                print(f"\n{n}: {self.cd.display_dict(row)}")


def main():
    dp = DataPlot()
    dp.dispatch()


if __name__ == '__main__':
    main()
This will read each row of the csv file into a dictionary format like:
Output:
AgencyID: Ambraseys-Finkel Time: 18.01.1900 15:30:00 Day: 18 Month: 01 Year: 1900 Hour: 15 Minute: 30 Second: 0 MiliSecond: 0 Latitude: 37.7900 Longitude: 28.2100 Depth: -10.0 Fixed: * Mtype: MS Magnitude: 4.40 M0: Rms: Herr: Verr: Gap: StationNO: Country: TÜRKÝYE City: AYDIN District: Yenipazar Town: Other: EventType: Eq
you will need CreateDict.py:
import os
 
class CreateDict:
    """
    Generic Software tools used by Trailmapper.
 
    CreateDict.py - Contains methods to simplify node and cell creation within
                    a dictionary
 
    Usage: 
     
        The best way to learn what can be done is to examine the testit function
        included in this module.
 
        new_dict(dictname) - Creates a new dictionary instance with the name
            contained in dictname
 
        add_node(parent, nodename) - Creates a new node (nested dictionary)
            named in nodename, in parent dictionary.
 
        add_cell(nodename, cellname, value) - Creates a leaf node within node
            named in nodename, with a cell name of cellname, and value of value.
 
        display_dict(dictname) - Recursively displays a nested dictionary.
 
    Requirements:
 
        Trailmapper software:
            None
 
        Python standard library:
            os
     
    Author: Larz60+  -- May 2019.
    """
    def __init__(self):
        os.chdir(os.path.abspath(os.path.dirname(__file__)))
 
    def new_dict(self, dictname):
        setattr(self, dictname, {})
 
    def add_node(self, parent, nodename):
        node = parent[nodename] = {}
        return node
 
    def add_cell(self, nodename, cellname, value):
        cell =  nodename[cellname] = value
        return cell
 
    def display_dict(self, dictname, level=0):
        indent = " " * (4 * level)
        for key, value in dictname.items():
            if isinstance(value, dict):
                print(f'\n{indent}{key}')
                level += 1
                self.display_dict(value, level)
            else:
                print(f'{indent}{key}: {value}')
            if level > 0:
                level -= 1
 
 
def testit():
    # instantiate class
    cd = CreateDict()
 
    # create new dictionary named CityList
    cd.new_dict('CityList')
 
    # add node Boston
    boston = cd.add_node(cd.CityList, 'Boston')
    # add sub node Resturants
    bos_resturants = cd.add_node(boston, 'Resturants')
 
    # Add subnode 'Spoke Wine Bar' to parent bos_resturants
    spoke = cd.add_node(bos_resturants, 'Spoke Wine Bar')
    cd.add_cell(spoke, 'Addr1', '89 Holland St')
    cd.add_cell(spoke, 'City', 'Sommerville')
    cd.add_cell(spoke, 'Addr1', '02144')
    cd.add_cell(spoke, 'Phone', '617-718-9463')
 
    # Add subnode 'Highland Kitchen' to parent bos_resturants
    highland = cd.add_node(bos_resturants, 'Highland Kitchen')
    cd.add_cell(highland, 'Addr1', '150 Highland Ave')
    cd.add_cell(highland, 'City', 'Sommerville')
    cd.add_cell(highland, 'ZipCode', '02144')
    cd.add_cell(highland, 'Phone', '617-625-1131')
 
    # display dictionary
    print(f'\nCityList Dictionary')
    cd.display_dict(cd.CityList)
    print(f'\nraw data: {cd.CityList}')
 
if __name__ == '__main__':
    testit()

Hi,
Thank you very much for your quick replay.
But, unless I misunderstood your answer, the problem is not related to reading the .csv file. There is a problem with drawing 3D graphics.

Hi again,
I solved the problem.
Before code:
for i in range(5, len(all_data)):
        if all_data[0:3][i][3] < 3.0:
            color = 'grey'
            marker_size = 1 * all_data[0:3][i][3]
            label = '$0 < M < 3.0$'
        elif 3.0 <= all_data[0:3][i][3] < 4.0:
            color = 'blue'
            marker_size = 2 * all_data[0:3][i][3]
            label = '$3.0 <= M < 4.0$'
        elif 4.0 <= all_data[0:3][i][3] < 5.0:
            color = 'black'
            marker_size = 3 * all_data[0:3][i][3]
            label = '$4.0 <= M < 5.0$'
        elif 5.0 <= all_data[0:3][i][3] < 6.0:
            color = 'limegreen'
            marker_size = 4 * all_data[0:3][i][3]
            label = '$5.0 <= M < 6.0$'
        elif 6.0 <= all_data[0:3][i][3] < 7.0:
            color = 'lightcoral'
            marker_size = 5 * all_data[0:3][i][3]
            label = '$6.0 <= M < 7.0$'
        elif all_data[0:3][i][3] >= 7.0:
            color = 'red'
            marker_size = 7 * all_data[0:3][i][3]
            label = '$M >= 7.0$'
 
        ax.scatter(all_data[0:3][i][0], all_data[0:3][i][1], all_data[0:3][i][2], marker='o', label=label, color=color, s=marker_size)
After code:
    for i in range(len(all_data)):

        if all_data[i][3] < 3.0:
            color = 'grey'
            marker_size = 1 * all_data[i][3]
            label = '$0 < M < 3.0$'
        elif 3.0 <= all_data[i][3] < 4.0:
            color = 'blue'
            marker_size = 2 * all_data[i][3]
            label = '$3.0 <= M < 4.0$'
        elif 4.0 <= all_data[i][3] < 5.0:
            color = 'black'
            marker_size = 3 * all_data[i][3]
            label = '$4.0 <= M < 5.0$'
        elif 5.0 <= all_data[i][3] < 6.0:
            color = 'limegreen'
            marker_size = 4 * all_data[i][3]
            label = '$5.0 <= M < 6.0$'
        elif 6.0 <= all_data[i][3] < 7.0:
            color = 'lightcoral'
            marker_size = 5 * all_data[i][3]
            label = '$6.0 <= M < 7.0$'
        elif all_data[i][3] >= 7.0:
            color = 'red'
            marker_size = 7 * all_data[i][3]
            label = '$M >= 7.0$'

        ax.scatter(all_data[i][0], all_data[i][1], all_data[i][2], marker='o', color=color, s=marker_size)
Reply
#7
Glad you solved your problem. I knew that reading the file was not the issue, I was just showing a different way to do so.
Thanks for sharing your solution.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  IndexError: list index out of range dolac 4 401 Jul-25-2022, 03:42 PM
Last Post: deanhystad
  I'm getting a String index out of range error debian77 7 593 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  IndexError: list index out of range Anldra12 2 581 May-03-2022, 01:39 PM
Last Post: Anldra12
  TypeError: list indices must be integers or slices, not range Anldra12 2 708 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  matplotlib x axis range goes over the set range Pedroski55 5 1,361 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  Python Error List Index Out of Range abhi1vaishnav 3 1,116 Sep-03-2021, 08:40 PM
Last Post: abhi1vaishnav
  IndexError: list index out of range Laplace12 1 1,455 Jun-22-2021, 10:47 AM
Last Post: Yoriz
  IndexError: list index out of range brunolelli 11 3,602 Mar-25-2021, 11:36 PM
Last Post: brunolelli
  index of range, but data prints out mrc06405j 1 1,368 Mar-25-2021, 07:20 PM
Last Post: buran
  Changing Index of 2 List in python giddyhead 0 1,061 Mar-05-2021, 05:45 PM
Last Post: giddyhead

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020