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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:15:26  来源:igfitidea点击:

pandas DataFrame diagonal

pythonnumpypandas

提问by piRSquared

What is an efficient way to get the diagonal of a square DataFrame. I would expect the result to be a Serieswith a MultiIndexwith two levels, the first being the index of the DataFramethe 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.diagwill 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 DataFramector.

np.diag将为您提供对角线值作为 np 数组,然后您可以通过压缩索引和列来构造多索引,并将其作为所需的索引传递给DataFramector。

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 iatin 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