Why am I getting vals=[vals[j] for j in usecols] IndexError: list index out of range

Hi , in my dat file i have 3 columns of data however the 3rd column is not loading when i include it in the code however when i delete it the other 2 columns do show. Thankyou! is there an easier way?? This is my code:

from matplotlib import pyplot as plt
import numpy as np
import scipy.optimize as opt
import pylab


for i in range(1):
    N=np.loadtxt(my_files[i],dtype=float, unpack=True, skiprows=1, usecols=(0))
    Mag=np.loadtxt(my_files[i],dtype=float, unpack=True, skiprows=1, usecols=(1))
    Mag_err=np.loadtxt(my_files[i],dtype=float, unpack=True, skiprows=1, usecols=(2))

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

Please use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks are not single quotes.


Welcome, vadgh.

Try removing (setting to False) the unpack argument.

Hi sky020 It worked thanks!

Is the code in your original post the exact code you are trying to run? If so, take the values out of the for loop, and change to this my_files='earthquake_data.dat', and remove the indexing. See if that changes any thing.