![](/img/trans.png)
[英]Iterating through columns in pandas while applying different functions to each column
[英]Pandas: Use rowname while applying lambda on each column
我正在尝试对数据帧中的所有列进行操作时使用索引(或行名)。 以下是我的数据框的结构:
gene 6 6 6 6 6 6 8 8 8 10 ... 28 67 67 67 67 67 67 35 35 35
mn:1:chr1:un 0 1 0 0 0 0 3 0 1 2 ... 17 8 8 6 8 7 14 9 17 15
pl:1:chr1:un 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
mn:2:chr1:un 1 0 0 0 0 1 0 0 0 0 ... 16 2 3 4 3 6 12 11 10 4
mn:3:chr1:un 7 16 10 9 8 7 11 10 15 9 ... 295 153 130 173 194 187 181 265 269 271
我想做的是应用某种标准化函数,如下所示:
count = count.apply(lambda x: (x * 114 * 1000000) / (np.sum(x) * lengthDict[rowname]), axis=0)
简化:
dataframe = for each element in dataframe: {perform some operation involving constant on element ÷ (sum of column containing element × dictionary[row index])}
其中count是我的数据框,而x应该是每一列中的单个元素。 这里的问题是lengthDict
,这是一个字典,其中包含每行的数值。 我试图以某种方式使用元素的列总和乘以lengthDict
返回的值,该值取决于索引。 我尝试使用x.name
但它返回列的名称。 有有效的方法吗?
编辑:这是lengthDict
的结构- {'mn:1:chr1:un': 1680,'mn:2:chr1:un': 1000,'mn:3:chr1:un': 10040,'pl:1:chr1:un': 2960,'mn:5:chr1:un': 14000}
。 它实质上是将索引映射到数值。
以下是我初始化和设置数据框本身的方式:
count = pd.read_csv("count.csv")
count = count.set_index('gene')
Intended output:
gene 6 6 6 6 6 6 8 8 8 10 ... 28 67 67 67 67 67 67 35 35 35
mn:1:chr1:un 0.000000 16.534392 0.000000 0.000000 0.000000 0.000000 29.614697 0.000000 10.126420 27.466967 ... 9.467610 9.224107 9.082131 6.759914 6.741892 5.856967 11.921943 5.707930 10.533360 9.566057
pl:1:chr1:un 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 ... 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
mn:2:chr1:un 27.893320 0.000000 0.000000 0.000000 0.000000 32.167043 0.000000 0.000000 0.000000 0.000000 ... 14.969962 3.874125 5.721743 7.571104 4.247392 8.434032 17.167597 11.720283 10.409438 4.285593
mn:3:chr1:un 19.447534 44.267375 28.098445 28.521137 25.638344 22.427221 18.169974 16.413099 25.416912 20.682298 ... 27.490903 29.518980 24.695436 32.614565 27.357040 26.181341 25.791294 28.122737 27.889829 28.919219
使用x.index
产生以下错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-78-da4ea45fc265> in <module>()
9 #count = count.T
---> 10 count = count.apply(lambda x: (x * 114 * 1000000) / (np.sum(x) * lengthDict[x.index]), axis=0)
11 count = count.groupby(by=count.columns, axis=1).median()
/anaconda3/lib/python3.7/site-packages/pandas/core/frame.py in apply(self, func, axis, broadcast, raw, reduce, result_type, args, **kwds)
6012 args=args,
6013 kwds=kwds)
-> 6014 return op.get_result()
6015
6016 def applymap(self, func):
/anaconda3/lib/python3.7/site-packages/pandas/core/apply.py in get_result(self)
316 *self.args, **self.kwds)
317
--> 318 return super(FrameRowApply, self).get_result()
319
320 def apply_broadcast(self):
/anaconda3/lib/python3.7/site-packages/pandas/core/apply.py in get_result(self)
140 return self.apply_raw()
141
--> 142 return self.apply_standard()
143
144 def apply_empty_result(self):
/anaconda3/lib/python3.7/site-packages/pandas/core/apply.py in apply_standard(self)
246
247 # compute the result using the series generator
--> 248 self.apply_series_generator()
249
250 # wrap results
/anaconda3/lib/python3.7/site-packages/pandas/core/apply.py in apply_series_generator(self)
275 try:
276 for i, v in enumerate(series_gen):
--> 277 results[i] = self.f(v)
278 keys.append(v.name)
279 except Exception as e:
<ipython-input-78-da4ea45fc265> in <lambda>(x)
9 #count = count.T
10 #count = (count * 114 * 1000000) / (genes[5] * count.sum())
---> 11 count = count.apply(lambda x: (x * 114 * 1000000) / (np.sum(x) * lengthDict[x.index]), axis=0)
12 #count = count.T
13 count = count.groupby(by=count.columns, axis=1).median()
/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py in __hash__(self)
2060
2061 def __hash__(self):
-> 2062 raise TypeError("unhashable type: %r" % type(self).__name__)
2063
2064 def __setitem__(self, key, value):
TypeError: ("unhashable type: 'Index'", 'occurred at index 6')
我决定使用一种更原始且不太优雅的方法。 这是代码:
sumCount = count.sum()
sumCount = sumCount.tolist()
count = count * (fragLength * 1000000)
length = count.index.to_series().map(lengthDict)
length = length.tolist()
scaleMatrix = np.zeros(shape=(len(sumCount),len(length)))
for i in range(0, len(sumCount)):
for k in range(0, len(length)):
scaleMatrix[i,k] = sumCount[i] * length[k]
scaleDataframe = pd.DataFrame(data = scaleMatrix.T, columns=count.columns, index=count.index)
count = count.divide(scaleDataframe)
我没有直接对数据框进行操作,而是创建了一个单独的数据框,其中包含缩放因子,然后将原始数据框除以“ scalingFactor”数据框。 这似乎可行,但仍然无法回答为什么我在使用lambda / apply时无法访问行名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.