1

I am trying to put my values into two arrays and then to make them a dataframe. I am using python, numpy and pandas to do so.

my arrays are:

k = [7.0, 8.0, 6.55, 7.0000001, 10.12]
p = [6.94, 9.0, 4.44444, 13.0, 9.0876]

and I would like to put them into a pandas dataframe. When I print my dataframe, I would like to see this:

    a     b    c     d     e
k  7.0   8.0  6.6   7.0  10.1
p  6.9   9.0  4.4  13.0   9.1

How can I do that?

I read some related questions, but I can't get it right. One of the errors says that indexes must not be tuples, but, as you can see, I don't have tuples

2
  • 7
    pd.DataFrame(np.vstack((k,p)),index=['k','p']) ? Commented Jun 18, 2020 at 17:29
  • Great! Thank you very much!!! Commented Jun 18, 2020 at 17:30

2 Answers 2

7

You can always have as input to pd.DataFrame be a list of lists, which will generate the output you desire:

k = [7.0, 8.0, 6.55, 7.0000001, 10.12]
p = [6.94, 9.0, 4.44444, 13.0, 9.0876]

pd.DataFrame([k,p],columns=['a','b','c','d','f'],index=['k','p'])

    a   b   c   d   e
k   7.00    8.0 6.55000 7.0 10.1200
p   6.94    9.0 4.44444 13.0    9.0876

And if you want rounded:

pd.DataFrame([k,p],columns=['a','b','c','d','f'],index=['k','p']).round()

    a   b   c   d   e
k   7.0 8.0 7.0 7.0 10.0
p   7.0 9.0 4.0 13.0    9.0

for dynamic columns:

from string import ascii_lowercase
pd.DataFrame([k,p],columns=list(ascii_lowercase[:len(k)]),index=['k','p']).round()
Sign up to request clarification or add additional context in comments.

3 Comments

is it possible also to round the results?
pd.DataFrame([k,p],columns=['a','b','c','d','f'],index=['k','p']).round() This could help.
you can import ascii_lowercase from the string module to make the column naming dynamic
0

Thanks to other answers I got this:

df = pd.DataFrame([k,p],columns=['a','b','c','d','f'],index=['k','p']).round(decimals=2)

How to get it step-by-step:

1) put arrays into another array by writing array = [k,p]. By printing it, result should look like this:

[[7.0, 8.0, 6.55, 7.0000001, 10.12],[6.94, 9.0, 4.44444, 13.0, 9.0876]]

2) write my column names into another array, arraycolnames. By printing it, result should look like this: ['a','b','c','d','e']

3) write my indexes names into another array, arrayindnames. By printing it, result should look like this: ['k','p']

4) create my dataframe by writing:

df = pd.Dataframe(array, columns=arraycolnames, index=arrayindnames)

5) round it using .round() method. By adding decimals=2 I say I want decimals till the 2nd, but I can write any number of decimal places needed

df.round(decimals=2)

My final result, as I print df, is:

      a      b      c      d       e
k   7.00   8.00   6.55   7.00  10.12
p   6.94   9.00   4.44  13.00   9.09

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.