1
我的指数:如何将索引转换为列表?
Index([u'Newal', u'Saraswati Khera', u'Tohana'], dtype='object')
我有这种格式转换成清单,格式如下:
['Newal','SaraswatiKhera','Tohana']
我的指数:如何将索引转换为列表?
Index([u'Newal', u'Saraswati Khera', u'Tohana'], dtype='object')
我有这种格式转换成清单,格式如下:
['Newal','SaraswatiKhera','Tohana']
您可以使用tolist
或list
:
print df.index.tolist()
print list(df.index)
但最快的解决方法是转换np.arry
由values
tolist
(谢谢EdChum)
print df.index.values.tolist()
样品:
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']
如果您需要编码UTF-8
:
print [x.encode('UTF8') for x in idx.tolist()]
['Newal', 'Saraswati Khera', 'Tohana']
另一种解决方案:
print [str(x) for x in idx.tolist()]
['Newal', 'Saraswati Khera', 'Tohana']
,但它会失败,如果unicode字符串的字符不要躺在地上e ascii范围。
时序:
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
很好的总结,注意使用打印的方式会比较长(根据%timeit〜的3.5倍)来运行 – ysearka
好主意,我添加它。 – jezrael
如果性能是关键,使用底层np数组''df.index.values.tolist()'这将比其他方法更快 – EdChum