pandas:获取一行索引的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40004871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pandas: get the value of the index for a row?
提问by Richard
I have a dataframe:
我有一个数据框:
cost month para
prod_code
040201060AAAIAI 43 2016-01-01 0402
040201060AAAIAJ 45 2016-02-01 0402
040201060AAAIAI 46 2016-03-01 0402
040201060AAAIAI 41 2016-01-01 0402
040201060AAAIAI 48 2016-02-01 0402
How can I iterate over the rows, and get the value of the index for each one?
如何遍历行,并获取每一行的索引值?
d = { 'prod_code': ['040201060AAAIAI', '040201060AAAIAJ', '040201060AAAIAI', '040201060AAAIAI', '040201060AAAIAI', '040201060AAAIAI', '040301060AAAKAG', '040301060AAAKAK', '040301060AAAKAK', '040301060AAAKAX', '040301060AAAKAK', '040301060AAAKAK'], 'month': ['2016-01-01', '2016-02-01', '2016-03-01', '2016-01-01', '2016-02-01', '2016-03-01', '2016-01-01', '2016-02-01', '2016-03-01', '2016-01-01', '2016-02-01', '2016-03-01'], 'cost': [43, 45, 46, 41, 48, 59, 8, 9, 10, 12, 15, 13] }
df = pd.DataFrame.from_dict(d)
df.set_index('prod_code', inplace=True)
This is what I'm trying:
这就是我正在尝试的:
for i, row in df.iterrows():
print row.index, row['cost']
But I get this:
但我明白了:
Index([u'items', u'cost'], dtype='object') 3.34461552621
UPDATE: This is the same as asking how to get the name of the index for a series, but phrased differently. Also though the answeris the same as another question, the questionis not the same! Specifically, this question will be found when people Google for "pandas index of row" rather than "pandas name of series".
更新:这与询问如何获取系列的索引名称相同,但措辞不同。此外,虽然答案与另一个问题相同,但问题并不相同!具体来说,当人们搜索“pandas index of row”而不是“pandas name of series”时,会发现这个问题。
回答by Steven G
for i, row in df.iterrows():
returns a Series
for each row where the Series
name is the index
of the row you are iterating through. you could simply do
Series
为每一行返回一个,其中Series
名称是index
您正在迭代的行的名称。你可以简单地做
d = { 'prod_code': ['040201060AAAIAI', '040201060AAAIAJ', '040201060AAAIAI', '040201060AAAIAI', '040201060AAAIAI', '040201060AAAIAI', '040301060AAAKAG', '040301060AAAKAK', '040301060AAAKAK', '040301060AAAKAX', '040301060AAAKAK', '040301060AAAKAK'], 'month': ['2016-01-01', '2016-02-01', '2016-03-01', '2016-01-01', '2016-02-01', '2016-03-01', '2016-01-01', '2016-02-01', '2016-03-01', '2016-01-01', '2016-02-01', '2016-03-01'], 'cost': [43, 45, 46, 41, 48, 59, 8, 9, 10, 12, 15, 13] }
df = pd.DataFrame.from_dict(d)
df.set_index('prod_code', inplace=True)
for i, row in df.iterrows():
print(row.name, row['cost'])
040201060AAAIAI 43
040201060AAAIAJ 45
040201060AAAIAI 46
040201060AAAIAI 41
040201060AAAIAI 48
040201060AAAIAI 59
040301060AAAKAG 8
040301060AAAKAK 9
040301060AAAKAK 10
040301060AAAKAX 12
040301060AAAKAK 15
040301060AAAKAK 13
you can learn more about it here
你可以在这里了解更多信息
回答by nishant kumar
use this to iterate over any value df.ix[row_value,col_value]
for finding the column index use this function
使用它来迭代任何值df.ix[row_value,col_value]
以查找列索引使用此函数
def find_column_number(column_name):
x=list(df1.columns.values)
print column_name
col_lenth= len(x)
counter=0
count=[]
while counter<col_lenth:
if x[counter]==column_name:
count=counter
counter+=1
return count