How to iterate over rows in a Dataframe in pandas (Python)?

0 votes
How to iterate over rows in a Dataframe in pandas (Python)?
May 2, 2018 in Data Analytics by DataKing99
• 8,100 points
2,265 views

7 answers to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Follow this code. hope it helps.

for index,row in df.iterrows():
    print(row['a'],row['b'])

answered May 2, 2018 by DeepCoder786
• 1,700 points
0 votes

You can use:

DataFrame.iterrows()

for index, row in df.iterrows():
    print row["c1"], row["c2"]
answered Dec 10, 2018 by Kishore
0 votes

You can also use :

DataFrame.itertuples()

for row in df.itertuples(index=True, name='Pandas'):
    print getattr(row, "c1"), getattr(row, "c2")

itertuples() is supposed to be faster than iterrows()

answered Dec 10, 2018 by Anurag
0 votes
itertuples is a good option.
answered Dec 10, 2018 by Ayman
0 votes

You can also use df.apply(), something like this:

def valuation_formula(x, y):
    return x * y * 0.5
df['price'] = df.apply(lambda row: valuation_formula(row['x'], row['y']), axis=1)
answered Dec 10, 2018 by Lyndsey
0 votes

You can use the df.iloc function as follows:

for i in range(0, len(df)):
    print df.iloc[i]['c1'], df.iloc[i]['c2']
answered Dec 10, 2018 by Farookh
0 votes

You can use IMHO:

for ind in df.index:
     print df['c1'][ind], df['c2'][ind]
answered Dec 10, 2018 by Ishaan

Related Questions In Data Analytics

0 votes
1 answer

How to sample n random rows per group in a dataframe?

You can assign a random ID to ...READ MORE

answered Jul 2, 2018 in Data Analytics by darklord
• 6,140 points
73 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How to replace NA values in a dataframe with Zero's ?

It is simple and easy: df1<-as.data.frame(matrix(sample(c(NA, 1:10), 100, ...READ MORE

answered Apr 10, 2018 in Data Analytics by CodingByHeart77
• 3,680 points
76 views
0 votes
2 answers
0 votes
1 answer

Converting a pandas data-frame to a dictionary

Emp_dict=Employee.to_dict('records') You can directly use the 'to_dict()' function ...READ MORE

answered May 23, 2018 in Data Analytics by Bharani
• 4,550 points
823 views
0 votes
3 answers

Python vs. R for data science

Hello Well there are lots of difference between ...READ MORE

answered Mar 30 in Data Analytics by MrBoot
• 1,210 points
65 views
0 votes
1 answer
0 votes
1 answer

How to drop rows of Pandas DataFrame whose value in certain coulmns is NaN

Easy Way: This Way we can easily handle ...READ MORE

answered May 7, 2018 in Data Analytics by DeepCoder786
• 1,700 points
576 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.