Reputation: 1168
i have a dataframe like so:
df = pd.DataFrame({0: [1, 4, 7, 10], 1: [5, 7, 9, 12], 2: ['v=55', 'g=40', 'd=84', 'f=31']})
s = pd.Series([0, 15, 30, 45])
df.set_index([s], inplace=True)
I want to iterate over the rows and get back:
I have tried using iterrows, to get the 2 indexes and the row:
ind=0
for index, row in df.iterrows():
if index==0: #continue to start loop from second value
continue
splitvalue= row[2].split('=')[1]
print ( ind, index, splitvalue) #print ind, next index, splitvalue
ind=index #update ind with current index
However the like this the splitvalue it returns is the next one and not the current one
current answer:
ind:0, index=15, splitvalue:'40'
ind:15, index=30, splitvalue:'84'....
wanted answer:
ind:0, index=15, splitvalue:'55'
ind:15, index=30, splitvalue:'40'....
Upvotes: 0
Views: 2466
Reputation: 1924
The issue is that you are getting the value for the row, you start printing at the second row, when you really want to start printing at the first row. You can use df.loc[ind]
to get the row at df.index == ind
.
ind=0
for index, row in df.iterrows():
if index==0: #continue to start loop from second value
continue
splitvalue = df.loc[ind][2].split('=')[1]
print (f"ind:{ind}, index:{index}, splitvalue:{splitvalue}") #print ind, next index, splitvalue
ind = index #update ind with current index
Let me know if that explanation makes sense.
Upvotes: 2