0

I have created a matrix in dataframe to compare words and have converted it to numpy using "penaltymatrix = np.array(df.values)". How do I convert the rows and columns also?

current output

The top part is the numpy array and the second part is the pandas dataframe which includes the appropiate column & row names.

Code:

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)
3
  • 2
    can you also tell us how your desired output should look? i mean if you want the headers of columns in dataframe as a list? Commented Jul 19, 2018 at 9:58
  • seems like you want index and columns also, use df.columns and df.index to get them Commented Jul 19, 2018 at 10:03
  • I need to write code to formulaically generate the table. I don't necessarily need the headers to show but I need to be able to manipulate specific elements. Commented Jul 19, 2018 at 10:03

1 Answer 1

0

Here is what you want:

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.