gpt4 book ai didi

python - 有没有一种方法可以根据值从 Pandas DataFrame 中提取索引

转载 作者:行者123 更新时间:2023-12-04 17:14:29 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Getting (index, column) pairs for True elements of a boolean DataFrame in Pandas

(3 个回答)


1年前关闭。




我有以下数据帧:

index col0 col1 col2
0 0 1 0
1 1 0 1
2 0 1 1

我想提取以下索引(那些包含一个(或任何值)的索引):
[(0, 1), (1, 0), (1, 2), (2, 1), (2,2))]

Pandas 中有没有可以做到这一点的方法?

最佳答案

使用 np.where + zip

[*zip(*np.where(df))]
[(0, 1), (1, 0), (1, 2), (2, 1), (2, 2)]

关于python - 有没有一种方法可以根据值从 Pandas DataFrame 中提取索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58452300/

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