Home > database >  How to isolate an item from a list that is part of a Series
How to isolate an item from a list that is part of a Series

Time:03-26

Hello I have a panda series named t1 that looks like this:

print(t1)

0         [21, 45]
1         [09, 45]
3         [19, 30]
4         [08, 00]
5         [23, 00]
            ...   
136935    [08, 20]
136936    [10, 20]
136937    [00, 15]
136938    [21, 00]
136939    [20, 05]

each row consists of a list with 2 items and I am trying to isolate the first item from each list and append it to an array but I am getting the error: IndexingError: Too many indexers

For example I know I can isolate the first item from the first row ('21') using the iloc like this (which works):

t1.iloc[0][0][:]

but I get the error IndexingError: Too many indexers when I try to put it in a for loop and try to append it to a new arrary which I dont understand.

arr = []
for i in t1.iteritems():
    arr.append(t1.iloc[i][0][:])

Hopefully someone can assist because I am lost. The code works outside a for loop but in the for loop I get the error.

CodePudding user response:

Use .str[0]:

arr = t1.str[0].tolist()

CodePudding user response:

iloc expects an location index (integer). iteritems does not return integers, but items. Since you didn't provide full minimally working code, I cannot verify, but you could probably do this:

for i in range(len(t1)):
    arr.append(t1.iloc[i][0][:])

That being said, you seem to be trying to do something that's counter-productive when using a DataFrame.

  • Related