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