pandas 熊猫数据帧对角线
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37310264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pandas DataFrame diagonal
提问by piRSquared
What is an efficient way to get the diagonal of a square DataFrame
. I would expect the result to be a Series
with a MultiIndex
with two levels, the first being the index of the DataFrame
the second level being the columns of the DataFrame
.
什么是有效的方式来获得一个正方形的对角线DataFrame
。我希望得到的结果是Series
一个MultiIndex
有两个层次,第一个是指数DataFrame
的第二水平面的列DataFrame
。
Setup
设置
import pandas as pd
import numpy as np
np.random.seed([3, 1415])
df = pd.DataFrame(np.random.rand(3, 3) * 5,
columns = list('abc'),
index = list('ABC'),
dtype=np.int64
)
I want to see this:
我想看这个:
print df.stack().loc[[('A', 'a'), ('B', 'b'), ('C', 'c')]]
A a 2
B b 2
C c 3
回答by johnchase
If you don't mind using numpy you could use numpy.diag
如果你不介意使用 numpy 你可以使用 numpy.diag
pd.Series(np.diag(df), index=[df.index, df.columns])
A a 2
B b 2
C c 3
dtype: int64
回答by EdChum
You could do something like this:
你可以这样做:
In [16]:
midx = pd.MultiIndex.from_tuples(list(zip(df.index,df.columns)))
pd.DataFrame(data=np.diag(df), index=midx)
Out[16]:
0
A a 2
B b 2
C c 3
np.diag
will give you the diagonal values as a np array, you can then construct the multiindex by zipping the index and columns and pass this as the desired index in the DataFrame
ctor.
np.diag
将为您提供对角线值作为 np 数组,然后您可以通过压缩索引和列来构造多索引,并将其作为所需的索引传递给DataFrame
ctor。
Actually the complex multiindex generation doesn't need to be so complicated:
其实复杂的多索引生成不需要这么复杂:
In [18]:
pd.DataFrame(np.diag(df), index=[df.index, df.columns])
Out[18]:
0
A a 2
B b 2
C c 3
But johnchase's answeris neater
但约翰切斯的回答更简洁
回答by Alexander
You can also use iat
in a list comprehension to get the diagonal.
您还可以iat
在列表理解中使用以获取对角线。
>>> pd.Series([df.iat[n, n] for n in range(len(df))], index=[df.index, df.columns])
A a 2
B b 2
C c 3
dtype: int64