I have a pandas dataframe that looks like this:
df =
| Index | 0 | 1 | 2 | 3 |
|---|---|---|---|---|
| Sam | 123 | 125 | 234 | 245 |
| Mary | 142 | 244 | 355 | 664 |
| Tom | 124 | 577 | 466 | 114 |
| Mark | 155 | 677 | 123 | 224 |
And I want to use a for loop by using every pair of an index and an item from the dataframe. By idx, item in the code, I meant: (Sam, 123), (Sam, 125), (Sam, 234), (Sam, 245), (Mary, 142), (Mary, 244), (Mary, 355), (Mary, 664), (Tom, 124), .... , (Mark, 224)
list = []
for idx, item in df:
new_list = [function1(idx), function2(item)]
list.append(new_list)
Maybe for loop with two variables from dataframe might not be the best approach to get the list of results using two functions. Any tips will be very helpful. Thank you!
CodePudding user response:
defnitely not the efficient one but !!works!!
l=[]
x=df.stack()
for i in range(len(x)):
l.append([x.index[i][0],x[i]])
print(l)
output:
[['Sam', 123], ['Sam', 125], ['Sam', 234], ['Sam', 245], ['Mary', 142], ['Mary', 244], ['Mary', 355], ['Mary', 664], ['Tom', 124], ['Tom', 577], ['Tom', 466], ['Tom', 114], ['Mark', 155], ['Mark', 677], ['Mark', 123], ['Mark', 224]]
