在pandas DataFrame的每一列中查找第一个非零值 [英] Find first non-zero value in each column of pandas DataFrame
本文介绍了在pandas DataFrame的每一列中查找第一个非零值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在数据框的每一列(从上到下)中获取第一个非零元素的值和索引的潘多拉式方法是什么?
What is a pandoric way to get a value and index of the first non-zero element in each column of a DataFrame (top to bottom)?
import pandas as pd
df = pd.DataFrame([[0, 0, 0],
[0, 10, 0],
[4, 0, 0],
[1, 2, 3]],
columns=['first', 'second', 'third'])
print(df.head())
# first second third
# 0 0 0 0
# 1 0 10 0
# 2 4 0 0
# 3 1 2 3
我想要实现的目标:
# value pos
# first 4 2
# second 10 1
# third 1 3
推荐答案
You're looking for idxmax
which gives you the first position of the maximum. However, you need to find the max of "not equal to zero"
df.ne(0).idxmax()
first 2
second 1
third 3
dtype: int64
We can couple this with lookup
and assign
df.ne(0).idxmax().to_frame('pos').assign(val=lambda d: df.lookup(d.pos, d.index))
pos val
first 2 4
second 1 10
third 3 3
相同答案的包装略有不同.
Same answer packaged slightly differently.
m = df.ne(0).idxmax()
pd.DataFrame(dict(pos=m, val=df.lookup(m, m.index)))
pos val
first 2 4
second 1 10
third 3 3
这篇关于在pandas DataFrame的每一列中查找第一个非零值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文