How to get dataframe index from series?

こ雲淡風輕ζ 提交于 2019-12-10 18:21:46

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/39988903/how-to-get-dataframe-index-from-series

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!