本文介绍了在pandas.DataFrame的对角线上设置值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个熊猫数据框,我想将对角线设置为0
I have a pandas dataframe I would like to se the diagonal to 0
import numpy
import pandas
df = pandas.DataFrame(numpy.random.rand(5,5))
df
Out[6]:
0 1 2 3 4
0 0.536596 0.674319 0.032815 0.908086 0.215334
1 0.735022 0.954506 0.889162 0.711610 0.415118
2 0.119985 0.979056 0.901891 0.687829 0.947549
3 0.186921 0.899178 0.296294 0.521104 0.638924
4 0.354053 0.060022 0.275224 0.635054 0.075738
5 rows × 5 columns
现在我想将对角线设置为0:
now I want to set the diagonal to 0:
for i in range(len(df.index)):
for j in range(len(df.columns)):
if i==j:
df.loc[i,j] = 0
df
Out[9]:
0 1 2 3 4
0 0.000000 0.674319 0.032815 0.908086 0.215334
1 0.735022 0.000000 0.889162 0.711610 0.415118
2 0.119985 0.979056 0.000000 0.687829 0.947549
3 0.186921 0.899178 0.296294 0.000000 0.638924
4 0.354053 0.060022 0.275224 0.635054 0.000000
5 rows × 5 columns
但是必须有比这更Python化的方式!?
but there must be a more pythonic way than that!?
推荐答案
In [21]: df.values[[np.arange(df.shape[0])]*2] = 0
In [22]: df
Out[22]:
0 1 2 3 4
0 0.000000 0.931374 0.604412 0.863842 0.280339
1 0.531528 0.000000 0.641094 0.204686 0.997020
2 0.137725 0.037867 0.000000 0.983432 0.458053
3 0.594542 0.943542 0.826738 0.000000 0.753240
4 0.357736 0.689262 0.014773 0.446046 0.000000
请注意,这仅在df
具有与列相同的行数时才有效.适用于任意形状的另一种方法是使用 np.fill_diagonal :
Note that this will only work if df
has the same number of rows as columns. Another way which will work for arbitrary shapes is to use np.fill_diagonal:
In [36]: np.fill_diagonal(df.values, 0)
这篇关于在pandas.DataFrame的对角线上设置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!