由於難以解釋的原因,我想要平均使用隨機值稀疏填充的熊貓數據框中的單元塊。數據框將始終有sqrt(列數×索引數)值 - 其餘全部爲NaN。這些值大致是均勻分佈的,所以如果我平均分配正確大小的單元格塊,我希望在每個塊內都有一個值。對熊貓數據框中的單元格進行平均處理
這是我的例子。對於100列和100個索引,我有100個值隨機分佈在整個數據框中。我預計每個10x10區塊會有〜1個值,其他都是NaN。我怎樣才能把各10×10塊爲一個單元格(內它平均的10列,10個指數,和值(S)
我的代碼:?
import pandas as pd
import numpy as np
import math
number_of_planes = 100
thicknesses = np.empty(number_of_planes)
cos_thetas = np.empty(number_of_planes)
phis = np.empty(number_of_planes)
for i in range(0,number_of_planes):
r = 1
phi = np.random.uniform(0,2*math.pi)
theta = math.acos(2*np.random.uniform(0.5,1) - 1)
thickness = np.random.uniform(0,0.4)
phis[i] = phi
cos_thetas[i] = math.cos(theta)
thicknesses[i] = thickness
thick_df = pd.DataFrame(columns=phis, index=cos_thetas)
for i in range(0, len(thicknesses)):
thick_df.set_value(cos_thetas[i], phis[i], thicknesses[i], takeable=False)
thick_df = thick_df.sort_index(axis=0, ascending=False)
thick_df = thick_df.sort_index(axis=1)
好吧,我想我明白,但是當我試圖把它應用到我的代碼沒有奏效。我需要更改哪部分以便爲我的100x100數據幀進行這項工作? – Arnold
@Rebecca你能否詳細說明沒有工作的部分?那裏的NaNs或價值不匹配?應該沒有任何變化的工作。 – Divakar
我得到運行時警告說「空片的平均值」和結果數據幀看起來沒有變化。 – Arnold