将Pandas Dataframe行和列转换为Numpy数组

时间:2018-07-19 09:53:21

标签: python arrays python-3.x pandas numpy

我在数据框中创建了一个矩阵来比较单词,并使用“ penaltymatrix = np.array(df.values)”将其转换为numpy。如何也转换行和列?

current output

顶部是numpy数组,第二部分是pandas数据框,其中包含适当的列和行名称。

代码:

def computeTable():
    sampInput = file1()
    refInput = file2()
    sampString = [word.strip(string.punctuation).lower() for word in sampInput.split()]
    refString = [word.strip(string.punctuation).lower() for word in refInput.split()]
    df = pd.DataFrame(index=sampString, columns=refString)


    penaltymatrix = np.array(df.values)
    penaltymatrix[0, 0] = 0
    print(penaltymatrix)
    print(3*"\n")
    print(df)

1 个答案:

答案 0 :(得分:0)

这是您想要的:

def computeTable():
    sampInput = file1()
    refInput = file2()
    sampString = [word.strip(string.punctuation).lower() for word in sampInput.split()]
    refString = [word.strip(string.punctuation).lower() for word in refInput.split()]
    df = pd.DataFrame(index=sampString, columns=refString)

    penaltymatrix = np.hstack([np.array([0]+list(df.index)).reshape((-1, 1)), np.vstack([np.array(df.columns), df.values])])
    print(penaltymatrix)
    print(3*"\n")
    print(df)