Python Forum
How to transform array into dataframe or table? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: How to transform array into dataframe or table? (/thread-17082.html)



How to transform array into dataframe or table? - python_newbie09 - Mar-27-2019

I would like to transform the array below into a dataframe with two columns. The values on the right will be column 1 and the values on the left will be column 2.

[[ 0 219]
[ 1 330977]
[ 2 1621]
[ 3 480]
[ 4 1560902]
[ 5 130]
[ 6 295]
[ 8 0]
[ 9 8683]
[ 10 0]
[ 14 8050]
[ 16 0]]

Thank you.


RE: How to transform array into dataframe or table? - scidam - Mar-29-2019

s = """
[[ 0 219]
[ 1 330977]
[ 2 1621]
[ 3 480]
[ 4 1560902]
[ 5 130]
[ 6 295]
[ 8 0]
[ 9 8683]
[ 10 0]
[ 14 8050]
[ 16 0]]
"""

import numpy as np
data = np.mat(s).reshape(2, int(np.prod(np.mat(s).shape) / 2)).T
# Also, you can turn it into Data Frame:
import pandas as pd
df = pd.DataFrame(data)



RE: How to transform array into dataframe or table? - python_newbie09 - Mar-29-2019

(Mar-29-2019, 03:50 AM)scidam Wrote: s = """
[[ 0 219]
[ 1 330977]
[ 2 1621]
[ 3 480]
[ 4 1560902]
[ 5 130]
[ 6 295]
[ 8 0]
[ 9 8683]
[ 10 0]
[ 14 8050]
[ 16 0]]
"""

import numpy as np
data = np.mat(s).reshape(2, int(np.prod(np.mat(s).shape) / 2)).T
# Also, you can turn it into Data Frame:
import pandas as pd
df = pd.DataFrame(data)

Thats great! thanks a lot!