繁体   English   中英

在没有 for 循环的情况下,将包含 if 的函数应用于 pandas 中数据帧的每一行

[英]Apply a function including if to each row of a dataframe in pandas without for loop

给定一个数据框,我想获取每行的非零值,然后找到绝对值的最小值。 我想要一个用户定义的函数来为我做这件事。 另外,我不想使用任何 for 循环,因为数据很大。

我的尝试

np.random.seed(5)
data = np.random.randn(16)
mask = np.random.permutation(16)[:6]
data[mask] = 0
df = pd.DataFrame(data.reshape(4,4))

          0         1         2         3
0  0.441227 -0.330870  2.430771  0.000000
1  0.000000  1.582481 -0.909232 -0.591637
2  0.000000 -0.329870 -1.192765  0.000000
3  0.000000  0.603472  0.000000 -0.700179


def udf(x):
  if x != 0:
    x_min = x.abs().min()
  return x_min
df.apply(udf, axis=1)

我得到ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

问题我该如何解决上述问题?

期望的答案如下:

0.330870
0.591637
0.329870
0.603472

您可以使用x.ne(0)作为布尔索引来过滤行

res = df.apply(lambda x: x[x.ne(0)].abs().min(), axis=1)
print(res)

0    0.330870
1    0.591637
2    0.329870
3    0.603472
dtype: float64

或使用min(axis=1)

res = df[df.ne(0)].abs().min(axis=1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM