Python Forum

Full Version: [pandas] TypeError: list indices must be integers or slices, not str but type is int.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm reading from an .ods file randomly selected cells. I randomly select them by the file loc(index).at('the column name')
I created this list [('L', 1), ('L', 6), ('L', 7), ('C', 1), ('C', 6), ('C', 7)] and put the second value into loc() and the first value into at().
I get the error below saying it needs to be an integer but when I print out the type() of the second value it says it is an integer.

def get_exercise(file, m_g):
    df = pd.read_excel(file)
    df_len = (len(df))
    rand_nums = random.sample(range(0, df_len + 1), 3 )
    ix_mg = [(mg, rn) for mg in m_g for rn in rand_nums]
    pprint(ix_mg) 
    pprint(type(ix_mg[0][1]))
                
    exercises = df.loc[ix_mg[0][1]].at[m_g[ix_mg[0][0]]]
    pprint(exercises)
returned values
Output:
[('L', 1), ('L', 6), ('', 7), ('C', 1), ('C', 6), ('C', 7)] <class 'int'>
Error:
Traceback (most recent call last): File "/home/cspower/python_projects/D_Ex_R.py", line 49, in <module> main() File "/home/cspower/python_projects/D_Ex_R.py", line 45, in main get_ex(file, m_g) File "/home/cspower/python_projects/D_Ex_R.py", line 35, in get_ex ex= df.loc[ix_mg[0][1]].at[m_g[ix_mg[0][0]]] TypeError: list indices must be integers or slices, not str
It looks like ix_mg[0][0] is a string. It is probably the cause of the error message.
In the line
exercises = df.loc[ix_mg[0][1]].at[m_g[ix_mg[0][0]]]
Look at this part
m_g[ix_mg[0][0]]
Not used pandas in a while is that a valid input for at?

Looking at
https://pandas.pydata.org/docs/reference...me.at.html Wrote:Get value within a Series
df.loc[5].at['B']
This seems like what you are doing so ignore me
(Dec-29-2023, 09:31 PM)cspower Wrote: [ -> ]I'm reading from an .ods file randomly selected cells. I randomly select them by the file loc(index).at('the column name')
I created this list [('L', 1), ('L', 6), ('L', 7), ('C', 1), ('C', 6), ('C', 7)] and put the second value into loc() and the first value into at().
I get the error below saying it needs to be an integer but when I print out the type() of the second value it says it is an integer.

def get_exercise(file, m_g):
    df = pd.read_excel(file)
    df_len = (len(df))
    rand_nums = random.sample(range(0, df_len + 1), 3 )
    ix_mg = [(mg, rn) for mg in m_g for rn in rand_nums]
    pprint(ix_mg) 
    pprint(type(ix_mg[0][1]))
                
    exercises = df.loc[ix_mg[0][1]].at[m_g[ix_mg[0][0]]]
    pprint(exercises)
returned values
Output:
[('L', 1), ('L', 6), ('', 7), ('C', 1), ('C', 6), ('C', 7)] <class 'int'>
Error:
Traceback (most recent call last): File "/home/cspower/python_projects/D_Ex_R.py", line 49, in <module> main() File "/home/cspower/python_projects/D_Ex_R.py", line 45, in main get_ex(file, m_g) File "/home/cspower/python_projects/D_Ex_R.py", line 35, in get_ex ex= df.loc[ix_mg[0][1]].at[m_g[ix_mg[0][0]]] TypeError: list indices must be integers or slices, not str

This code show it is an int
pprint(type(ix_mg[0][1]))
returned value
Output:
<class 'int'>
(Dec-29-2023, 10:39 PM)cspower Wrote: [ -> ]This code show it is an int

1 pprint(type(ix_mg[0][1]))
Try pprint(type(ix_mg[0][0])) . Details matter.