Moritz
Moritz

Reputation: 5408

pandas indexing in multiindex dataframe

i do have an excel file:

<>   1    2    3
A
B
C

with my data in each cell.

in another sheet i do have my description:

    name    pH    salt    id
A1    sample    8.5    50    1
A2    sample    8.5    50    1
A3    sample    8.5    50    2
B1    sample    7.5    50    2

reading the data and the labels:

d = pd.read_excel(data_name,'280', index_col = 0)
d_label =pd.read_excel(data_name,'label', index_col = 0)

creating a list from the data:

data = list(chain.from_iterable(d.values.tolist()))

making a multiindex:

index = pd.MultiIndex.from_tuples(tuple(d_label.values), names=d_label.columns)

when I created a series:

s = pd.Series(data, index = index)

I can now index the series as usual. E.g.

s['some_name']

But if i do:

df = pd.DataFrame(data, index = index)

and:

df['some_name']

i get an error:

'no item named some_name'

What did I wrong ?

Upvotes: 1

Views: 202

Answers (1)

joris
joris

Reputation: 139162

See the basic indexing documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#basics

When indexing a Series, s['some_name'] will access the row with label some_name.
While when indexing a DataFrame, df['some_name'] will access the column (and not row index) with label some_name. You can do df.loc['some_name'] to access a row.

Upvotes: 1

Related Questions