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

Time:08-03

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]

CodePudding user response:

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()]

CodePudding user response:

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))
  • Related