我想用列中值替換列中值以上的所有值。改進代碼以取代中位數值大於中值的大熊貓DataFrame
這裏是我的數據框:
m = pd.DataFrame({
'a': xrange(5),
'b': xrange(5, 10),
'c': xrange(10,15)})
print m
a b c
0 0 5 10
1 1 6 11
2 2 7 12
3 3 8 13
4 4 9 14
這裏是我的解決方案:
for col in m.columns:
quart = m[col].median()
m[col] = [val if val < quart else quart for val in m[col]]
print m
a b c
0 0 5 10
1 1 6 11
2 2 7 12
3 2 7 12
4 2 7 12
我不熟悉的數據幀,所以我在想,如果有可能做到這一點更'熊貓「的方式或使用一些花式的線性代數。
非常感謝您的回覆。
編輯答案:
下面是分別從hurrial和chrisb的解決方案的快速timeit:
%timeit m.apply(lambda col: np.where(col.median() < col, col.median(), col))
1000 loops, best of 3: 1.36 ms per loop
%timeit np.minimum(m, m.median())
1000 loops, best of 3: 400 µs per loop
使用np.minimum的解決方案似乎更快。
謝謝我今天學到了2個強大的東西,np.where和np.minimum!