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

标签 python pandas scipy

我有以下数据框:

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/

相关文章:

python - 给定段的长度和段中的偏移量,如何从 Pandas 的开始创建偏移量?

python - Pandas 按日期过滤值

python - 如何限制Kaze发现的关键点数量?

python - 为什么我的 2D 插值器在 SciPy 中生成一个交换轴的矩阵?

python - 在 python 中创建独立于平台的 GUI 可执行文件

python - 计算两个日期之间的秒数差异不适用于一天以上

python - BS HTML 解析 - & 在打印 URL 字符串时被忽略

python - 从其他数据框按行查找

python - 在组内联合非集合迭代的有效方法

python - 添加到常规 numpy ndarray 时调用 scipy.sparse __add__ 方法?