Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
480 views
in Technique[技术] by (71.8m points)

python - 如何在熊猫数据框中的多个列之间进行比较?(How to compare between multiple columns in a pandas dataframe?)

So, I have a sample dataset from which I have extracted the correlation matrix and for each column, I have calculated the average correlation and now wish to compare both.

(因此,我有一个样本数据集,从中提取了相关矩阵,并针对每一列计算了平均相关性,现在希望将两者进行比较。)

For this purpose, I have written the following code in Python.

(为此,我用Python编写了以下代码。)

This code looks at both sets(correlation matrix and the average values) and eliminates all values above the average and then draws a graph between the rest.

(该代码将同时查看两组数据(相关矩阵和平均值),并消除所有高于平均值的值,然后在其余值之间绘制图形。)

    import matplotlib.pyplot as plt
    import pandas as pd
    import numpy as np
    import pylab as pl
    import networkx as nx
    from time import time

    filesrc = "F:Sumit2PRPTD.csv"

    Data = pd.read_csv(filesrc)

    #selecting the number of independent variables 

    n = 9

    mylist = list(Data.columns)
    limit_list = mylist[0:n]

    correlation_matrix = Data.corr(method = 'pearson')
    print(correlation_matrix)
    cmean = correlation_matrix.mean()

    Reduced_corrmat = correlation_matrix.iloc[0:n,0:n]
    Reduced_cmean = cmean.iloc[0:n,]

    G = nx.DiGraph()
    G.add_nodes_from([1,n])

    i = 0

    N = len(Reduced_corrmat.axes[1])

    for k in limit_list:
          for j in Reduced_corrmat.axes[1]:
              if Reduced_corrmat.iloc[k][j] < Reduced_cmean[k]:
                  G.add_edges_from([(k,j)])
                  nx.draw(G, with_labels = True)

While running this, I get the error, cannot do positional indexing on with these indexers [ambient] of .

(运行此代码时,出现错误,无法使用的这些索引器[环境]进行位置索引。)

Any idea on what the error is and how to solve it will be appreciated.

(关于错误是什么以及如何解决它的任何想法将不胜感激。)

  ask by sumitpal0593 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...