Python Forum
[pandas] TypeError: list indices must be integers or slices, not str but type is int.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[pandas] TypeError: list indices must be integers or slices, not str but type is int.
#1
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
Reply
#2
It looks like ix_mg[0][0] is a string. It is probably the cause of the error message.
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
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
Reply
#4
(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'>
Reply
#5
(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.
« We can solve any problem by introducing an extra level of indirection »
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: unhashable type: 'Series' bongielondympofu 2 538 Mar-14-2024, 06:12 PM
Last Post: deanhystad
  Numpy returns "TypeError: unsupported operand type(s) for *: 'numpy.ufunc' and 'int'" kalle 2 2,642 Jul-19-2022, 06:31 AM
Last Post: paul18fr
  New to Pandas. I need help fixing a TypeError kramon19 1 1,800 Dec-30-2020, 02:20 PM
Last Post: snippsat
  Comparing results within a list and appending to pandas dataframe Aryagm 1 2,358 Dec-17-2020, 01:08 PM
Last Post: palladium
  [pandas] How to reshape the list Mekala 6 7,438 Jul-26-2020, 12:49 AM
Last Post: Mekala
  Error Message: TypeError: unhashable type: 'set' twinpiques 4 19,009 May-22-2019, 02:31 PM
Last Post: twinpiques
  Error:unsupported operand type(s) for ** or pow(): 'list' and 'int' mcgrim 3 18,372 Mar-22-2019, 01:29 PM
Last Post: buran
  Inserting data from python list into a pandas dataframe mahmoud899 0 2,617 Mar-02-2019, 04:07 AM
Last Post: mahmoud899
  List and Dictionaries with Pandas Balinor 3 2,997 Aug-20-2018, 10:47 PM
Last Post: ichabod801
  missing pandas even if in conda list metalray 2 3,481 May-29-2018, 12:52 PM
Last Post: metalray

Forum Jump:

User Panel Messages

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