Python 如何在新数据帧中保留原始索引
所有这些CTYNAME在原始普查中都有不同的索引。我如何将它们转移到新的DF,以便得到如下答案:Python 如何在新数据帧中保留原始索引,python,pandas,dataframe,Python,Pandas,Dataframe,所有这些CTYNAME在原始普查中都有不同的索引。我如何将它们转移到新的DF,以便得到如下答案: def answer_eight(): templist = list() for county, region, p15, p14, ste, cty in zip(census_df.CTYNAME, census_df.REGION,
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
在开始筛选之前,可以使用以下命令将原始索引分配给列:
STNAME CTYNAME
12 Iowa Washington County
222 Minnesota Washington County
400 Pennsylvania Washington County
2900 Rhode Island Washington County
2999 Wisconsin Washington County
然后就把它当作你正在选择的其他列中的一列来对待。我会把索引和你正在压缩的其他内容一起包括进来
census_df['original index'] = census_df.index
似乎您需要
df=pd.DataFrame.from_记录(模板列表,列=标签,索引=普查\u df.index)
ValueError:传递值的形状是(2,5),索引暗示(23193)
生成:ValueError:传递值的形状是(2,5),索引暗示(23193)你似乎需要我看到这个线程,但它对我也不起作用:raise AttributeError(“只能使用带字符串的.str访问器”AttributeError:只能使用带字符串值的.str访问器,它在Pandastanks中使用np.object dtype,我实现了census_df['original index']=census_df.index在筛选之前,但它不会更改结果。如果我将index=census_df['original index']添加到新的FD创建中,它将返回:ValueError:传递值的形状为(2,5),索引暗示(23193)您是何时得到该错误的?当您更改`templast.append((ste,cty))时
到模板列表。追加((ste,cty,原始索引列))
?如果我这样做,我得到了正确的结果,但如何使用最后一个值作为索引?您的数据帧名称。重置索引(“原始索引列”)
打印([k[2]用于模板列表中的k])生成[89614191234523553163]
但是当我在reset\u idnex语句中使用它时,它返回相同的结果:df=pd.DataFrame.from\u records(templast)
df.reset\u index([k[2]表示templast中的k])
def answer_eight():
templist = list()
index = list()
zipped = zip(
census_df.CTYNAME,
census_df.REGION,
census_df.POPESTIMATE2015,
census_df.POPESTIMATE2014,
census_df.STNAME,
census_df.CTYNAME,
census_df.index
)
for county, region, p15, p14, ste, cty, idx in zipped:
# print(county)
if region == 1 or region == 2:
if county.startswith('Washington'):
if p15 > p14:
templist.append((ste, cty))
index.append(idx)
labels = ['STNAME', 'CTYNAME']
df = pd.DataFrame(templist, index, labels)
return df.rename_axis(census_df.index.name)