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']
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]
However, when I attempt to fill the ks
series, I get the following error:
ValueError: object too deep for desired array
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?