Convert multiple Int64Index([], dtype='int64') in a list to a list

Question:

I have a list with int64index, I want to change it to a flat list. Can you help me with this? here is a simple example.

l = [Int64Index([518], dtype='int64'), Int64Index([599], dtype='int64'), Int64Index([614], dtype='int64')]

The output:

[518, 599, 614]
Asked By: Sadcow

||

Answers:

You should probably fix whatever process produced this in the first place, but you can get a list of python int objects using something like:

[x for idx in l for x in idx.tolist()]
Answered By: juanpa.arrivillaga

You can use np.ravel and tolist() to get the flattened python list.

np.ravel(l).tolist()

If you want pandas Index:

pd.Index(np.ravel(l))
Answered By: SomeDude
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.