简体   繁体   中英

How to solve IndexError: list index out of range?

I have the following piece of code:

s = output.set_index('name')['col1']
df = pd.DataFrame(s.values.tolist(), index=s.index).stack().reset_index()

The second line raises an error:

IndexError: list index out of range

I just want to understand why it happens?

The s.index returns:

Index(['100100', '100200', '100300'], dtype='object', name='name')

The s.values.tolist() returns:

[
 [],
 [],
 []
]

Update:

This is an example of s . It should be a starting point.

      col1
a     []
b     []
c     []
d     ["c1","c2"]

When col1 is empty in all rows, the code fails at df = pd.DataFrame(s.values.tolist(), index=s.index).stack().reset_index() .

This code fails for me (python 3.7 and pandas 0.24.2):

s = pd.DataFrame({'name':['a','b','c'],
                 'col1': [[],[],[]]}).set_index('name')
s.col1.apply(pd.Series).stack().dropna().reset_index()

The goal is to get either empty DataFrame if all col1 values are [], or the following DataFrame (for the above-shown example of s ):

df =

name  col1
d     c1
d     c2

s.values.tolist() gives [[], [], [], ['c1', 'c2']] , which is not really what you want. I think you need pd.Series instead of tolist :

s = pd.DataFrame({'name':['a','b','c','d','e'],
                 'col1': [[],[],[],['c1','c2'],['d','e','f']]}).set_index('name')
s.col1.apply(pd.Series).stack().dropna().reset_index()

Output:

+---+------+---------+----+
|   | name | level_1 | 0  |
+---+------+---------+----+
| 0 | d    |       0 | c1 |
| 1 | d    |       1 | c2 |
| 2 | e    |       0 | d  |
| 3 | e    |       1 | e  |
| 4 | e    |       2 | f  |
+---+------+---------+----+

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM