How to read line by line in a data frame using a loop and return each value or pull each value

How to read line by line Dataframeby the loop and return each value or cause each value?

Example:

id    name

1     abc
2     vbs
2     askj
+4
source share
1 answer

Assuming you are using pandas dataframes

you can use iterrows

In [3]: for index, row in df.iterrows():
   ...:     print('Index is: {}'.format(index))
   ...:     print('ID is: {}; Name is: {}'.format(row['id'], row['name']))
   ...:     
Index is: 0
ID is: 1; Name is: abc
Index is: 1
ID is: 1; Name is: vbs
Index is: 2
ID is: 2; Name is: askj

Iterrows iterates over an index, a tuple of strings. The line here is Series .

In [4]: type(row)
Out[4]: pandas.core.series.Series
+2
source

Source: https://habr.com/ru/post/1584035/


All Articles