Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use tuple as index in pandas Series

Tags:

python

pandas

I have a very simple task. Essentially, I want to create a pandas Series and use tuple values as index. For example,

series_tmp = pd.Series()
series_tmp[(0,'a')] = 1

What I want to do is that, I want to create one more row in the pd.Series, whose index is (0,'a') and whose value is 1.

The above code gets the error:

KeyError: '[0 1] not in index'

Any help?

I know about multi-index, but it cannot help my case. Because I need to have very complex tuples like ('a',(2,'c'),'d') as a key.

Conclusion: Thanks for all the wonderful answers! To add a row with tuple as index, we should do:

series_tmp = series_tmp.append(pd.Series([1],index=[(0,'a')]))
like image 998
user40780 Avatar asked Oct 28 '17 03:10

user40780


People also ask

Can you use a tuple as an index?

Indexing Tuples As an ordered sequence of elements, each item in a tuple can be called individually, through indexing. Each item corresponds to an index number, which is an integer value, starting with the index number 0 .

Can pandas series have index?

Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Pandas Series.

Can we convert tuple into series?

Creating a pandas Series from a tuple is similar to creating a Series from a list. Make the tuple which contains the required data, and then pass it to the data parameter of the series constructor.

Can you use ILOC on a series?

iloc attribute enables purely integer-location based indexing for selection by position over the given Series object. Example #1: Use Series. iloc attribute to perform indexing over the given Series object.


3 Answers

If you are creating a series object with a multi-index from data, you can do so by constructing a dictionary with tuples as keys, and the data as values. Then pass that to the series constructor.

import pandas as pd

d = {(0,'a'):1, (0,'b'):1.5, (1,'a'):3, (1,'b'):3.5}
s = pd.Series(d)
s
# returns:
0  a    1.0
   b    1.5
1  a    3.0
   b    3.5
dtype: float64

Edit based on comments:

For this situation, an index of explicit tuples is required. In that case, you can construct the index ahead of time, then use that as the index parameter when constructing the series.

ix = pd.Index([(1,'a'), ('a',(2,'b')), (2,('b',1))])
s = pd.Series(data=[1,5,9], index=ix)
s
# returns:
(1, a)         1
(a, (2, b))    5
(2, (b, 1))    9
dtype: int64

# check indexing into the series object
s[('a',(2,'b'))]
# returns:
5
like image 59
James Avatar answered Oct 27 '22 07:10

James


Try it like this:

df = pd.DataFrame(columns=['a', 'b'], index=pd.MultiIndex.from_tuples([('0', 'a'), ('1', 'b')]))

print(df)

Output:

       a    b
0 a  NaN  NaN
1 b  NaN  NaN
like image 27
kjmerf Avatar answered Oct 27 '22 06:10

kjmerf


In :series_tmp = pd.Series([5,6],index=[(0,'a'),(1,'b')])
    series_tmp
Out:(0, a)    5
    (1, b)    6
    dtype: int64
like image 1
Shihe Zhang Avatar answered Oct 27 '22 06:10

Shihe Zhang