如何解决从先前连接的数据框中选择带有for循环的第一个元素
我有一个由数据帧串联而成的数据帧:
df = pd.concat([df1,df2,df3],keys=[df1,names=['frame'])
,并希望将大框架中每个框架的列的第一项附加到列表中。
我尝试了将所有帧放入列表并从每一行中选择第一个元素的方法,但是出现了以下错误。
未实现使用DataFrame键为MultiIndex编制索引
编辑。:我尝试过的代码
keys = [df1,df3]
d = []
for key in keys:
d.append(df.loc[key]['col1'][0])
解决方法
使用keys
的{{1}}参数向多索引添加最外层并对其进行分组。
concat
在不相关的注释上,应该避免像在其中那样链接多个索引
df = pd.concat(
[df1,df2,df3],# list of objects to be concatenated
keys=['df1','df2','df3'],# list of strings to construct a new outermost level into the multiindex
names=['frame'] # name for the new level in the multiindex
# notice if you pass keys=[('df1','a'),('df1','b'),('df3','a')]
# and names=['real frame','variation']
# then the multiindex will have two new levels
)
first_rows = df.groupby('frame').first()
d = first_rows.col1.to_list()
相反,
.loc[key]['col1'][0]
...请注意,无论如何,您的呼叫仍然会失败,因为正在迭代的.loc[key,'col1']
是您尝试用作索引的数据帧列表
如何使用
kubectl run my-service --image=my-service-image:latest --port=8080 --image-pull-policy Never
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。