Home > Blockchain >  Plotting colour map from a list of arrays
Plotting colour map from a list of arrays

Time:03-30

I have a list of 12 arrays, each with 18 values which looks like this:

m = array([[0.01799641],
        [0.01799641],
        [0.01799641],
        [0.01799641],
        [0.01922812],
        [0.01922812],
        [0.01922812],
        [0.01922812],
        [0.01691673],
        [0.01691673],
        [0.01691673],
        [0.01691673],
        [0.01762076],
        [0.01762076],
        [0.01762076],
        [0.01762076],
        [0.01140901],
        [0.01140901]], dtype=float32),
 array([[0.01799641],
        [0.01799641],
        [0.01799641],
        [0.01799641],
        [0.01922812],
        [0.01922812],
        [0.01922812],
        [0.01922812],
        [0.01691673],
        [0.01691673],
        [0.01691673],
        [0.01691673],
        [0.01762076],
        [0.01762076],
        [0.01762076],
        [0.01762076],
        [0.01140901],
        [0.01140901]], dtype=float32),
 array([[0.01799641],
        [0.01799641],
        [0.01799641],
        [0.01799641],
        [0.01922812],
        [0.01922812],
        [0.01922812],
        [0.01922812],
        [0.01691673],
        [0.01691673],
        [0.01691673],
        [0.01691673],
        [0.01762076],
        [0.01762076],
        [0.01762076],
        [0.01762076],
        [0.01140901],
        [0.01140901]], dtype=float32)

I'm trying to turn this into a pandas dataframe, where each array is a new row of the dataframe:

enter image description here

However, I've tried the following:

df = pd.DataFrame(,data=m, dtype=object)

and get the following error:

ValueError: Must pass 2-d input. shape=(12, 18, 1)

Anyone know a better way to do it?

CodePudding user response:

Try squeeze to remove the last dimension from (12, 18, 1):

df = pd.DataFrame(np.array(m).squeeze())
print(df)

# Output
         0         1         2   ...        15        16        17
0  0.017996  0.017996  0.017996  ...  0.017621  0.011409  0.011409
1  0.017996  0.017996  0.017996  ...  0.017621  0.011409  0.011409
2  0.017996  0.017996  0.017996  ...  0.017621  0.011409  0.011409

[3 rows x 18 columns]

CodePudding user response:

The problem is that your input array has the wrong dimension (12,18,1). But you need a 2D dimension for a pandas data frame (12,18). You have some possibilities:

  1. You can either reshape the array m.
m = np.reshape(m, [-1, 18]) # -1 means, that the number of rows will be automatically determined
df = pd.DataFrame(data=m, dtype=object)
  1. Not only that, but you could also initialize directly to the right shape:
m = np.array(
    [
        [
            0.01799641,
            0.01799641,
            0.01799641,
            0.01799641,
            0.01922812,
            0.01922812,
            0.01922812,
            0.01922812,
            0.01691673,
            0.01691673,
            0.01691673,
            0.01691673,
            0.01762076,
            0.01762076,
            0.01762076,
            0.01762076,
            0.01140901,
            0.01140901,
        ],

…additional rows…

        [
            0.01799641,
            0.01799641,
            0.01799641,
            0.01799641,
            0.01922812,
            0.01922812,
            0.01922812,
            0.01922812,
            0.01691673,
            0.01691673,
            0.01691673,
            0.01691673,
            0.01762076,
            0.01762076,
            0.01762076,
            0.01762076,
            0.01140901,
            0.01140901,
        ],
    ]
)
df = pd.DataFrame(data=m, dtype=object)

Notice how the values are not each in [] but are simply float values.

  • Related