Dan Lo
Dan Lo

Reputation: 201

How to get dataframe index from series?

Say I extract a series from a dataframe (like what would happen with an apply function). I'm trying to find the original dataframe index from that series.

df=pd.DataFrame({'a':[1,2,3],'b':[4,5,6],'c':[7,8,9]})
x=df.ix[0]
x


Out[109]: 
a    1
b    4
c    7
Name: 0, dtype: int64

Notice the "Name: 0" piece at the bottom of the output. How can I get the value '0' from series object x?

Upvotes: 3

Views: 658

Answers (1)

piRSquared
piRSquared

Reputation: 294198

you access it with the name attribute

x.name

0

take these examples

for i, row in df.iterrows():
    print row.name, i

0 0
1 1
2 2

Notice that the name attribute is the same as the variable i which is supposed to be the row index.

It's the same for columns

for j, col in df.iteritems():
    print col.name, j

a a
b b
c c

Upvotes: 1

Related Questions