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:
Solution 1:
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 |
+---+------+---------+----+
Post a Comment for "How To Solve Indexerror: List Index Out Of Range?"