Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert Index into list? [duplicate]

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']
like image 785
Sai Rajesh Avatar asked May 12 '16 06:05

Sai Rajesh


People also ask

How to convert the index to a list in Python?

And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course Example #1: Use Index.tolist () function to convert the index into a list. Let’s convert the index into a List. Example #2: Use Index.tolist () function to convert the index into a python list.

How do I convert a single index to a column?

So far you have seen how to convert a single index to a column. Alternatively, you may have a DataFrame with MultiIndex. Here is an example of a DataFrame with MultiIndex: You’ll now see the MultiIndex: You can then convert the MultiIndex into multiple columns using this code: You’ll now get the two new columns:

How to print the index of a list of data?

You can use tolistor list: print df.index.tolist() print list(df.index) But the fastest solution is convert np.arryby valuestolist(thanks EdChum) print df.index.values.tolist()

How to return a list of values from a pandas index?

Pandas Index.tolist () function return a list of the values. These are each a scalar type, which is a Python scalar (for str, int, float) or a pandas scalar (for Timestamp/Timedelta/Interval/Period). Example #1: Use Index.tolist () function to convert the index into a list.


Video Answer


1 Answers

You can use tolist or list:

print df.index.tolist()  print list(df.index) 

But the fastest solution is convert np.arry by values tolist (thanks EdChum)

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:

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.

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 
like image 51
jezrael Avatar answered Oct 06 '22 15:10

jezrael