Home > Software engineering >  With three lists, two of which are array coordinates, how do I create an array in python?
With three lists, two of which are array coordinates, how do I create an array in python?

Time:12-15

I have three lists (really columns in a pandas dataframe) one with data of interest, one with x array coordinates, and one with y array coordinates. All lists are the same length and their order in the list associated with the coordinates (so L1: "Apple" coincides with L2:"1", and L3:"A"). I would like to make an array with the dimensions provided by the two coordinate lists with data from the data list. What is the best way to do this?

The expected output would be in the form of a numpy array or something like:

array = [[0,0,0,3,0,0,2,3][0,0,0,0,0,0,0,3]] #databased on below

Where in this example the array has the dimensions of y = 2 from y.unique() and x = 8 from x.unique().

The following is example input data for what I am talking about:

array_x array_y Data
1 a 0
2 a 0
3 a 0
4 a 3
5 a 0
6 a 0
7 a 2
8 a 3
1 b 0
2 b 0
3 b 0
4 b 0
5 b 0
6 b 0
7 b 0
8 b 3

CodePudding user response:

Supposing you have a dataframe like that:

import pandas as pd
import numpy as np
myDataframe = pd.DataFrame([[1,2],[3,4],[5,6]], columns=['x','y'])

Then you can select the columns you want and creat an array from it

my_array = np.array(myDataframe[['x','y']])


>>> my_array
array([[1, 2],
       [3, 4],
       [5, 6]], dtype=int64)

CodePudding user response:

You may be looking for pivot:

df = pd.DataFrame({'Error_Type': lst1, 'array_x': lst2, 'array_y': lst3})
out = df.pivot(values=['Error_Type'], columns=['array_y'], index=['array_x']).values.T
  • Related