gpt4 book ai didi

pandas - 反向循环遍历数据帧行

转载 作者:行者123 更新时间:2023-12-05 05:11:05 27 4
gpt4 key购买 nike

我试图以相反的顺序遍历数据框行。

基于行位置而不是索引名称。

我虽然这段代码应该可以工作,但实际上不行。

for i, row in enumerate(df[::-1].iterrows()):  
print (i)

当我运行它时,它会产生

0
1
2
3
4
5

而不是

5
4
3
2
1
0

最佳答案

如果你接受重新索引,你也可以

for i, row in enumerate(df.reindex().sort_index(ascending=False):  
print (i)

关于pandas - 反向循环遍历数据帧行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55922358/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com