-3

I'm trying to write a loop to return df.iloc[0], df.iloc[1], df.iloc[2] ... df.iloc[the last row] so that every row can be fed to another function. Thanks!

MC2020
  • 67
  • 6
  • You mean `range(n)` ? – khelwood Aug 03 '22 at 14:53
  • [`range`](https://docs.python.org/3/library/stdtypes.html#range)…? – deceze Aug 03 '22 at 14:53
  • Thanks for your replies. I just did a quick google, and it seems range doesn't work for me? Actually, I want to loop over every row of a dataframe (i.e., from 1st row to the last). For the question, I want to do `df.iloc[0], df.iloc[1], df.iloc[2] ... df.iloc[the last row]`. – MC2020 Aug 03 '22 at 15:03

2 Answers2

0

Something like this?

import pandas as pd

d = {'col1': [1, 2], 'col2': [3, 4]}
df = pd.DataFrame(data=d)
for ind in df.index:
    print(df.iloc[ind])
fra96
  • 43
  • 6
  • Hi, thanks for the answer. Similar but not exactly? I'd like to return the rows so that the data can be passed into another function. It seems `print` doesn't work? Let me know if it's clear. Thanks! – MC2020 Aug 03 '22 at 15:29
  • yes print is only for an example. def function(element): # function code print(element) --> function(df.iloc[ind]) – fra96 Aug 03 '22 at 15:50
0

You could use a dataframe.apply() approach and set the axis value = 1:

import pandas as pd

Test = pd.DataFrame(
    {
     "Col1":[1, 2, 3, 4],
     "Col2":[11, 12, 13, 14]
     }
    )

Test["Row_multiply"] = Test.apply(
    lambda row: row["Col1"]*row["Col2"], axis=1
    )

Result:

   Col1  Col2  Row_multiply
0     1    11            11
1     2    12            24
2     3    13            39
3     4    14            56

I just gave the example of a simple multiplication between row values, but you can get fairly complex. If you provide an example (minimally reproducible example) I can use whatever function you need.

Luca
  • 50
  • 7
  • Thanks so much! Still a bit confused. My function is `learn.predict()` from fastai package. The function will take a row of a df and return 3 objects. The thirs object (a `tensor`) is of my interest. For example, `learn.predict(df.iloc[0])[2]` will work for one row. I want to get all the predicted value, from the first row to the last row, stacked in the form of a 1d `np.array`. `df.apply(learn_predict, axis = 1)` will return groups of objects, not only the **thrid** object that I'd like to have. How can I pick up the third object and convert it from tensors to a 1d `np.array`? Thank you! – MC2020 Aug 04 '22 at 09:07
  • @MC2020 that may be because you're calling learn_predict on all rows rather than per row, try `df.apply(lambda row: learn.predict(row), axis = 1)` see if that works for you – Luca Aug 04 '22 at 15:27