如何在查询返回中设置行数等于列名?

2024-03-29 11:23:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用SQLServer2019中的数据库内Python引擎计算一个大表中列之间的相关性,由于此计算返回一个对角矩阵,因此我希望能够在SSMS中查看标记为镜像列名的行的结果。你知道吗

我知道SQL查询的基本知识,但还不太了解,所以我可能没有精确地描述我的搜索。你知道吗

下面是我的代码示例:

execute sp_execute_external_script 
@language = N'Python',
@script = N'
import pandas as pd
from pandas import DataFrame

df = InputDataSet.corr()
OutputDataSet = df

',
@input_data_1 = N'select GHI ,
MNO,
JKL
from PIVOTED_TIME_ID_MATRIX'

with result sets ((GHI float,
MNO float,
JKL float))

这将返回:

***** GHI | MNO | JKL
Row 1   1   0.5   0.5
Row 2 0.5     1   0.5
Row 3 0.5   0.5     1 

我想看看:

***** GHI | MNO | JKL
GHI     1   0.5   0.5
MNO   0.5     1   0.5
JKL   0.5   0.5     1 

这可能吗?你知道吗


Tags: from引擎import数据库pandasdfexecutescript
1条回答
网友
1楼 · 发布于 2024-03-29 11:23:45

最后我把上面的建议结合起来使用测向列,以及一种从here重新排列列的方法,该方法提供了一个解决方案,以生成我所要的输出。你知道吗

...'
df = InputDataSet.corr()
#puts the names of the existing columns into a new column on the end of df
df["columns"] = df.columns 
cols = df.columns.tolist()
#shift the "columns" column to the front of the dataframe
cols = cols[-1:] + cols[:-1]
df = df[cols]
OutputDataSet = df

',
@input_data_1 = N'select GHI ,
MNO,
JKL
from PIVOTED_TIME_ID_MATRIX'

with result sets ((column_names varchar(max),  add a new column in the result set
GHI float,
MNO float,
JKL float))

相关问题 更多 >