擅长:python、mysql、java
<p>这个问题有点棘手。你知道吗</p>
<pre><code>data = pd.DataFrame({'a':[20, 5, 10], 'b':[15, 6, 15], 'c':[33, 10, 5], 'd':[5, 8, 10]}, index=['A', 'B', 'C'])
total = data.values.sum() # total sum
data['row_sum'] = data.sum(axis=1) # create a new column 'row_sum' containing sum of elements in that row
col_sum = data.sum(axis=0) # column sum
data = data.loc[:,'a':'d'].div(data['row_sum'], axis=0) # divide each cell with its row sum
data.loc['col_sum'] = col_sum # create a new row with corresponding column sum
data = data.loc['A':'C',:].div(data.loc['col_sum'], axis=1) # divide each cell with its column sum
def update(x):
return int(round(x*total*100)) # round number to nearest integer
data_new = data.applymap(update)
</code></pre>
<p>输出:</p>
<pre><code> a b c d
A 111 81 134 42
B 70 82 102 170
C 101 148 37 154
</code></pre>