def answer_eight(): templist = list() for county, region, p15, p14, ste, cty in zip(census_df.CTYNAME, census_df.REGION, census_df.POPESTIMATE2015, census_df.POPESTIMATE2014, census_df.STNAME, census_df.CTYNAME): # print(county) if region == 1 or region == 2: if county.startswith('Washington'): if p15 > p14: templist.append((ste, cty)) labels = ['STNAME', 'CTYNAME'] df = pd.DataFrame.from_records(templist, columns=labels) return df STNAME CTYNAME 0 Iowa Washington County 1 Minnesota Washington County 2 Pennsylvania Washington County 3 Rhode Island Washington County 4 Wisconsin Washington County All these CTYNAME has different indexes in the original census_df. How could I transfer them over to the new DF so the answer looks like:
STNAME CTYNAME 12 Iowa Washington County 222 Minnesota Washington County 400 Pennsylvania Washington County 2900 Rhode Island Washington County 2999 Wisconsin Washington County
df = pd.DataFrame.from_records(templist, columns=labels, index=census_df.index)