简体   繁体   English

Pandas Dataframe,更改“对角线”的值(其中index-value等于column-name)

[英]Pandas Dataframe, change values on “diagonal” (where index-value is equal to column-name)

I have a pandas DataFrame, where the index is a subset of the columns, ie each value in the index is also a column-name and there are additional columns, so something like this: 我有一个pandas DataFrame,其中索引是列的子集,即索引中的每个值也是一个列名,还有其他列,所以像这样:

import pandas as pd
import numpy as np
df = pd.DataFrame(index=['John', 'Mary', 'Steven'],
                  columns=['John','Susan','Steven','Chris','Mary'],
                  data=np.arange(15).reshape(3,5))

I now want to set the "diagonal" items to a fixed value, say 0.0. 我现在想要将“对角线”项设置为固定值,比如说0.0。 By "diagonal" I mean those entries, where the index-value matches the column name. “对角线”是指那些条目值与列名匹配的条目。 I can do it by iterating over the index and set each "diagonal" entry to 0.0 within that loop. 我可以通过迭代索引并在该循环中将每个“对角线”条目设置为0.0来实现。

for i in df.index:
    df.loc[i, i] = 0.0

But I wonder if there was a more pythonic way, ie maybe a vectorized of achieving this. 但我想知道是否有更多的pythonic方式,也许是实现这一点的矢量化。 I'm thinking that there must be something like df.loc[df.index, df.index] = 0.0 but that does not produce the desired results. 我认为必须有类似df.loc[df.index, df.index] = 0.0东西df.loc[df.index, df.index] = 0.0不会产生预期的结果。

you can use the numpy's fill_diagonal function 你可以使用numpy的fill_diagonal函数

import numpy as np 

#rearrange columns according to order of index
df = df.reindex(columns=list(df.index) + list(df.columns.difference(df.index))) 

np.fill_diagonal(df.values,0)

print df
         John  Mary  Steven  Chris  Susan
John       0     4       2      3      1
Mary       5     0       7      8      6
Steven    10    14       0     13     11

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pandas dataframe 比较没有列名引用的索引的所有列值 - Pandas dataframe compare all column values of index without column-name reference 如何按列名将分组设置为列而不是索引?(熊猫) - How to set group by column-name as column instead of index?(Pandas) 如何使用pandas数据框的列值更改numpy数组的索引值 - how to change the index value of numpy array with column values of pandas dataframe Pandas DataFrame根据列,索引值比较更改值 - Pandas DataFrame change a value based on column, index values comparison 根据索引使Pandas Dataframe列等于另一个Dataframe中的值 - Make Pandas Dataframe column equal to value in another Dataframe based on index Pandas 将列的所有值与不同的 DataFrame 进行比较,并返回值匹配的列名(不同的 DataFrame) - Pandas compare all values of a column with different DataFrame and return column name (of a dif. DataFrame) where value matches 查找最大值和列名的等效于此SQL的Python / pandas是什么? - What's the Python/pandas equivalent to this SQL for finding both the max value and column-name? 提取 dataframe 中的值,其中索引名称等于列名称 - Extract values in dataframe where index name equals to column name 将单个值添加到 pandas DataFrame wint 索引名称和列名称 - add single value to pandas DataFrame wint index name and column name 查找元素更改值熊猫数据框的索引 - Find index where elements change value pandas dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM