Richard
Richard

Reputation: 65510

pandas: get the value of the index for a row?

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 answer is the same as another question, the question is not the same! Specifically, this question will be found when people Google for "pandas index of row" rather than "pandas name of series".

Upvotes: 15

Views: 29827

Answers (2)

Steven G
Steven G

Reputation: 17122

for idx, row in df.iterrows():

returns a Series for each row where idx is the index of the row you are iterating through. you could simply do

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 idx, row in df.iterrows():
    print(idx, 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

Upvotes: 22

nishant kumar
nishant kumar

Reputation: 517

use this to iterate over any value df.ix[row_value,col_value] for finding the column index use this function

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

Upvotes: 0

Related Questions