简体   繁体   中英

How to do nested iterrows in Pandas

I am trying to take the data from the endResult dataframe'issues' column and put it into the 'Sprint' column in df. When I run this bit of code, it returns a dataframe that has the third entry from the 'issues' column inserted into each row of the 'Sprint' column in df.

for i, r in endResult.iterrows():
    j = endResult['issues'][i]['key']
for x, y in df.iterrows():
    df['Sprint'][x] = j

What I'm getting:

Sprint
0 SPGC-9445
1 SPGC-9445
2 SPGC-9445

What I should be getting:

Sprint
0 SPGC-14075
1 SPGC-9456
2 SPGC-9445

Entries are taken from endResult dataframe which contains json.

                                         issues
0  {u'key': u'SPGC-14075', u'fields': {u'status':...
1  {u'key': u'SPGC-9456', u'fields': {u'status': ...
2  {u'key': u'SPGC-9445', u'fields': {u'status': ...

Because you are assigning everything to j in the first loop, you overwrite this value on each loop. Then you assign each value in sprint to the value of j , which is going to be the last value in issues .

One simple change that fixes this is to change j to a list and append each value as you loop through. This also eliminates the second loop, since you can just make a column out of the created list:

import pandas as pd

endResult = pd.DataFrame({'issues' : [{'key': 1},{'key': 2},{'key': 3}]})
df = pd.DataFrame()

j = []
for i, r in endResult.iterrows():
    j.append(endResult['issues'][i]['key'])

df['Sprint'] = j

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM