How to iterate over row in a Dataframe in Pandas?

0 votes

I have a dataframe from pandas:

import pandas as pd
inp = [{'c1':1, 'c2':10}, {'c1':11,'c2':13}, {'c1':12,'c2':14}]
df = pd.DataFrame(inp)
print df
And the output is:
   c1   c2
0  1    10
1  11   13
2  12   14
Now I want to iterate over the rows of this frame. For every row I want to be able to access its elements (values in cells) by the name of the columns.
Jul 18, 2019 in Python by Shabnam
• 920 points
1,337 views

1 answer to this question.

0 votes

Hi,

You can use df.iterrows(), it yields both index and row.

Input:

import pandas as pd
import numpy as np
df = pd.DataFrame([{'c1':1, 'c2':10}, {'c1':11,'c2':13}, {'c1':12,'c2':14}])

for index, row in df.iterrows():
    print(row['c1'], row['c2'])

Output:

  1  10
  11 13
  12 14
answered Jul 18, 2019 by Taj
• 1,060 points

Related Questions In Python

0 votes
1 answer

How to convert a Pandas GroupBy object to DataFrame in Python

g1 here is a DataFrame. It has a hierarchical index, ...READ MORE

answered Nov 12, 2018 in Python by Nymeria
• 3,520 points
21,582 views
0 votes
1 answer

How to find if a value exists in Pandas dataframe?

Try this:​ for name in df['Name']: ...READ MORE

answered Apr 8, 2019 in Python by Tina
8,238 views
0 votes
1 answer

How to append a row to a Pandas dataframe?

You can use the append method provided by pandas ...READ MORE

answered May 9, 2019 in Python by Raj
988 views
0 votes
1 answer

How to iterate over a string when there is successive increase in its length?

The following code might help -  mystring = ...READ MORE

answered Jul 22, 2019 in Python by Arvind
• 2,980 points
60 views
0 votes
1 answer

How to rename columns in pandas (Python)?

You can use the rename function in ...READ MORE

answered Apr 30, 2018 in Data Analytics by DeepCoder786
• 1,720 points

edited Jun 8 by MD 446 views
0 votes
1 answer

What is the Difference in Size and Count in pandas (python)?

The major difference is "size" includes NaN values, ...READ MORE

answered Apr 30, 2018 in Data Analytics by DeepCoder786
• 1,720 points

edited Jun 8 by Gitika 1,148 views
0 votes
2 answers

Replacing a row in pandas data.frame

key error. I love python READ MORE

answered Feb 18, 2019 in Data Analytics by anonymous
6,503 views
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,580 points
3,155 views
0 votes
1 answer

How to create a train and test sample from one dataframe using pandas?

Hi, The below written code can help you ...READ MORE

answered Jul 3, 2019 in Python by Taj
• 1,060 points
3,038 views
0 votes
1 answer

How to convert multiple columns to string in pandas dataframe?

Hi, To convert multiple columns to string, include a list of ...READ MORE

answered Jul 18, 2019 in Python by Taj
• 1,060 points
12,260 views