我确实寻找了一个类似我的问题,但我仍然没有找到任何答案。
我有这样一个数据框架:
achaea bacteria plastids mitochondrion viruses
CTAG -22.141701 -27.891441 -2.474725 0.262533 0.026349
GGCC -13.403537 -21.490028 -0.403491 -0.271403 -0.243087
GATC -20.933825 -14.761891 4.681494 -0.098965 0.088650
CATG -8.490766 -9.910195 1.150736 -0.005730 0.508743
TAAG -17.376165 -18.653078 -1.525354 -0.708633 -1.917676我的疑问是:‘我如何才能从列中得到最小值和最大值?’我想要一些这样的值:
最小值:
archaea CTAG -22.141701
bacteria CTAG -27.891441
plastids CTAG -2.474725
mitochondrion TAAG -0.708633
viruses TAAG -1.917676最大值:
archaea CATG -8.4907661
bacteria CATG -9.910195
plastids GATC 4.681494
mitochondrion CTAG 0.262533
viruses CATG 0.508743我试过:
df.min()
achaea -22.141701
bacteria -27.891441
plastids -4.654833
mitochondrion -0.881587
viruses -1.917676
dtype: float64
df['achaea'].idxmin()
'CTAG'
df.reset_index().min()
index AAAA
achaea -22.1417
bacteria -27.8914
plastids -4.65483
mitochondrion -0.881587
viruses -1.91768
dtype: object嗯,我试过了,而且非常接近:
for col, idx in zip(df.columns, df.index):
print(df[col].min(), idx, col)
-22.141701229820306 CTAG archaea
-27.89144069672985 GGCC bacteria
-4.654832775512324 GATC plastids
-0.8815871622500514 CATG mitochondrion
-1.917675731085761 TAAG viruses发布于 2022-05-30 14:14:05
一个有趣的选项是agg,它提供了一个函数列表:
result = df.agg([min, max])关于你的数据样本我得到了:
achaea bacteria plastids mitochondrion viruses
min -22.141701 -27.891441 -2.474725 -0.708633 -1.917676
max -8.490766 -9.910195 4.681494 0.262533 0.508743但是,如果您希望min/max值为及其索引,那么:
iMin = col.idxmin() iMax = col.idxmax()返回pd.Series([coliMin,coliMax],index=[col.name,col.name,iMin,iMax])
结果= pd.concat([ xx(dfcol) in df ])
结果是:
achaea CTAG -22.141701
CATG -8.490766
bacteria CTAG -27.891441
CATG -9.910195
plastids CTAG -2.474725
GATC 4.681494
mitochondrion TAAG -0.708633
CTAG 0.262533
viruses TAAG -1.917676
CATG 0.508743
dtype: float64第一个索引级别显示列名。
第二级为本专栏中最小/最大值的指标值。
发布于 2022-05-30 14:12:18
您可以使用:
df.where(df.eq(df.min())).T.stack()。
产出:
achaea CTAG -22.141701
bacteria CTAG -27.891441
plastids CTAG -2.474725
mitochondrion TAAG -0.708633
viruses TAAG -1.917676
dtype: float64https://stackoverflow.com/questions/72435601
复制相似问题