gpt4 book ai didi

python - 如何在 Pandas 数据框上应用 scipy 函数

转载 作者:行者123 更新时间:2023-11-28 22:38:08 25 4
gpt4 key购买 nike

我有以下数据框:

import pandas as pd
import io
from scipy import stats

temp=u"""probegenes,sample1,sample2,sample3
1415777_at Pnliprp1,20,0.00,11
1415805_at Clps,17,0.00,55
1415884_at Cela3b,47,0.00,100"""
df = pd.read_csv(io.StringIO(temp),index_col='probegenes')
df

看起来像这样

                     sample1  sample2  sample3
probegenes
1415777_at Pnliprp1 20 0 11
1415805_at Clps 17 0 55
1415884_at Cela3b 47 0 100

我想做的也是执行row-zscore calculation using SCIPY .使用此代码我得到:

In [98]: stats.zscore(df,axis=1)
Out[98]:
array([[ 1.18195176, -1.26346568, 0.08151391],
[-0.30444376, -1.04380717, 1.34825093],
[-0.04896043, -1.19953047, 1.2484909 ]])

如何方便地附加列和索引名称还是那个结果?

在一天结束的时候。它看起来像:

                               sample1  sample2  sample3
probegenes
1415777_at Pnliprp1 1.18195176, -1.26346568, 0.08151391
1415805_at Clps -0.30444376, -1.04380717, 1.34825093
1415884_at Cela3b -0.04896043, -1.19953047, 1.2484909

最佳答案

documentation for pd.DataFrame有:

data : numpy ndarray (structured or homogeneous), dict, or DataFrame Dict can contain Series, arrays, constants, or list-like objects index : Index or array-like Index to use for resulting frame. Will default to np.arange(n) if no indexing information part of input data and no index provided columns : Index or array-like Column labels to use for resulting frame. Will default to np.arange(n) if no column labels are provided

所以,

pd.DataFrame(
stats.zscore(df,axis=1),
index=df.index,
columns=df.columns)

应该完成这项工作。

关于python - 如何在 Pandas 数据框上应用 scipy 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35912603/

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