对按行选择的 Pandas 列执行 scipy.stats 测试时出现 ValueError [英] ValueError When Performing scipy.stats test on Pandas Column Selection by Row
问题描述
目标是在存储 KS D 统计值的 Pandas 列中创建一个新列,df['ks']
.KS 统计数据是在该数据帧中的两组列之间生成的,grp1
和 grp2
:
The goal is to create a new column in a pandas column that stores the value of a KS D-statistic, df['ks']
. The KS statistic is generated between two groups of columns in that dataframe, grp1
and grp2
:
# sample dataframe
import pandas as pd
import numpy as np
dic = {'gene': ['x','y','z','n'],
'cell_a': [1, 5, 8,9],
'cell_b': [8, 5, 4,9],
'cell_c': [8, 6, 1,1],
'cell_d': [1, 2, 7,1],
'cell_e': [5, 7, 9,1],
}
df = pd.DataFrame(dic)
df.set_index('gene', inplace=True)
df['ks'] = np.nan
# sample groups
grp1 = ['cell_a','cell_b']
grp2 = ['cell_d','cell_e']
因此基因 x 的 D 统计量将是 stats.ks_2samp([1,5], [1,6])[0]
,基因 y 将是 stats.ks_2samp([5,2], [1,7])[0]
等尝试如下:
So the D-statistic for gene x would be stats.ks_2samp([1,5], [1,6])[0]
, gene y would be stats.ks_2samp([5,2], [1,7])[0]
, etc. Attempt is below:
# attempt 1 to fill in KS stat
for idx, row in df.iterrows():
df.ix[idx, 'ks'] = stats.ks_2samp(df[grp1], df[grp2])[0]
但是,当我尝试填充 ks
系列时,出现以下错误:
However, when I attempt to fill the ks
series, I get the following error:
ValueError: object too deep for desired array
我的问题有两个部分:1) 对象对于数组来说太深"是什么意思,以及 2) 如何在没有迭代的情况下完成同样的事情?
My question has two parts: 1) What does it mean for an object to be "too deep for an array", and 2) how can I accomplish the same thing without iteration?
推荐答案
循环中的 KS 计算出现太深"错误,因为我需要为每个分布传递一个一维数组以进行测试:>
The KS calculation in the loop was getting a "too deep" error because I needed to pass it a 1-D array for each distribution to test:
for idx, row in df.iterrows():
df.loc[idx, 'ks'] = stats.ks_2samp(df.loc[idx, grp1], (df.loc[idx, grp2]))[0]
我之前的尝试使用了二维数组.这就是导致它太深"的原因
My previous attempt used a 2-D array instead. That is what was causing it to be "too deep"
这篇关于对按行选择的 Pandas 列执行 scipy.stats 测试时出现 ValueError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!