Python Forum

Full Version: How to transform array into dataframe or table?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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)
(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!