【问题标题】:Extract data from a list in python从python中的列表中提取数据
【发布时间】:2021-06-22 04:08:32
【问题描述】:

我有以下清单:

[N               12.000000
 mean             2.011608
 median           2.021611
 std              0.572034
 relative std     0.284350
 dtype: float64,
 N               12.000000
 mean             2.011608
 median           2.021611
 std              0.571815
 relative std     0.284262
 dtype: float64,
 N               12.000000
 mean             2.011608
 median           2.021611
 std              0.572101
 relative std     0.284412
 dtype: float64,
 N               12.000000
 mean             2.011608
 median           2.021611
 std              0.572115
 relative std     0.284440
 dtype: float64,
 N               12.000000
 mean             2.011608
 median           2.021611
 std              0.571872
 relative std     0.284313
 dtype: float64]

我想从具有最小相对标准值的列表中提取数据(N、均值、标准、相对标准)。上面列表的输出应该如下:

 N               12.000000
 mean             2.011608
 median           2.021611
 std              0.571815
 relative std     0.284262
 dtype: float64

到目前为止我尝试了什么?

min(list)

但是抛出以下错误ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

【问题讨论】:

  • 您能以更易于复制/粘贴的方式分享您的列表吗?那是数据框列表吗?
  • 是的,它是一个数据框列表

标签: python pandas list minimum


【解决方案1】:

使用 min 和 lambda 函数通过标签 std 选择 Series 的值:

s1 = pd.Series([0,1,2], index=['std','min','max'])
s2 = pd.Series([4,1,2], index=['std','min','max'])
s3 = pd.Series([0.8,1,2], index=['std','min','max'])

L = [s1, s2, s3]

s = min(L, key=lambda x:x.loc['std'])
print (s)
std    0
min    1
max    2
dtype: int64

测试所有最小值:

print ([x.loc['std'] for x in L])
[0, 4, 0.8]

对于索引使用np.argmin:

import numpy as np
print (np.argmin([x.loc['std'] for x in L]))
0

【讨论】:

  • @MuSu18 - 需要最小值min(x.loc['std'] for x in lst)
  • 谢谢!是否可以提取最小值的索引?
  • 我试过 L.index(min(L, key=lambda x:x.loc['std'])) 它给了我一个错误说'ValueError:一个系列的真值是不明确的.使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。'
  • 它列出了std的所有值。输出如下所示[0.5717808890503777, 0.5721950145669352, 0.5721768182360689, 0.5720823409032569, 0.5721597650972318]
  • 非常感谢。哟是最棒的。我也试过(min(L, key=lambda x:x.loc['std'])).argmax() 那也行得通..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-05
  • 2019-01-20
  • 2010-10-14
  • 2020-11-19
  • 1970-01-01
  • 2021-03-22
  • 2021-10-14
相关资源
最近更新 更多