Python 如何将索引转换为列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37179155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:59:42  来源:igfitidea点击:

how to convert Index into list?

pythonpython-2.7pandas

提问by Sai Rajesh

My index:

我的指数:

Index([u'Newal', u'Saraswati Khera', u'Tohana'], dtype='object')

I have to convert this format into list with following format:

我必须将此格式转换为具有以下格式的列表:

['Newal','SaraswatiKhera','Tohana']

回答by jezrael

You can use tolistor list:

您可以使用tolistlist

print df.index.tolist()

print list(df.index)

But the fastest solution is convert np.arryby valuestolist(thanks EdChum)

但是,最快的解决方法是转换np.arry的 (感谢EdChumvaluestolist

print df.index.values.tolist()

Sample:

样本:

import pandas as pd

idx = pd.Index([u'Newal', u'Saraswati Khera', u'Tohana'])
print idx
Index([u'Newal', u'Saraswati Khera', u'Tohana'], dtype='object')

print idx.tolist()
[u'Newal', u'Saraswati Khera', u'Tohana']

print list(idx)
[u'Newal', u'Saraswati Khera', u'Tohana']

If you need encode UTF-8:

如果您需要编码UTF-8

print [x.encode('UTF8') for x in idx.tolist()]
['Newal', 'Saraswati Khera', 'Tohana']

Another solution:

另一种解决方案:

print [str(x) for x in idx.tolist()]
['Newal', 'Saraswati Khera', 'Tohana']

but it would fail if the unicode string characters do not lie in the ascii range.

但如果 unicode 字符串字符不在 ascii 范围内,它将失败。

Timings:

时间

import pandas as pd
import numpy as np

#random dataframe
np.random.seed(1)
df = pd.DataFrame(np.random.randint(10, size=(3,3)))
df.columns = list('ABC')
df.index = [u'Newal', u'Saraswati Khera', u'Tohana']
print df

print df.index
Index([u'Newal', u'Saraswati Khera', u'Tohana'], dtype='object')

print df.index.tolist()
[u'Newal', u'Saraswati Khera', u'Tohana']

print list(df.index)
[u'Newal', u'Saraswati Khera', u'Tohana']

print df.index.values.tolist()
[u'Newal', u'Saraswati Khera', u'Tohana']


In [90]: %timeit list(df.index)
The slowest run took 37.42 times longer than the fastest. This could mean that an intermediate result is being cached 
100000 loops, best of 3: 2.18 μs per loop

In [91]: %timeit df.index.tolist()
The slowest run took 22.33 times longer than the fastest. This could mean that an intermediate result is being cached 
1000000 loops, best of 3: 1.75 μs per loop

In [92]: %timeit df.index.values.tolist()
The slowest run took 62.72 times longer than the fastest. This could mean that an intermediate result is being cached 
1000000 loops, best of 3: 787 ns per loop