![]() |
[pandas] TypeError: list indices must be integers or slices, not str but type is int. - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: [pandas] TypeError: list indices must be integers or slices, not str but type is int. (/thread-41353.html) |
[pandas] TypeError: list indices must be integers or slices, not str but type is int. - cspower - Dec-29-2023 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
RE: .loc() error saying index msut be int when type is int. - Gribouillis - Dec-29-2023 It looks like ix_mg[0][0] is a string. It is probably the cause of the error message.
RE: [pandas] TypeError: list indices must be integers or slices, not str but type is int. - Yoriz - Dec-29-2023 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/api/pandas.DataFrame.at.html Wrote:Get value within a SeriesThis seems like what you are doing so ignore me RE: [pandas] TypeError: list indices must be integers or slices, not str but type is int. - cspower - Dec-29-2023 (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') This code show it is an int pprint(type(ix_mg[0][1]))returned value
RE: [pandas] TypeError: list indices must be integers or slices, not str but type is int. - Gribouillis - Dec-30-2023 (Dec-29-2023, 10:39 PM)cspower Wrote: This code show it is an intTry pprint(type(ix_mg[0][0])) . Details matter.
|