1

I have a 1x100 1D array that looks like :

(A1,B1,C1,D1),(A2,B2,C2.D2),(A3,B3,C3,D3) ..... (A100,B100,C100,D100)

I want to convert this array into a 4x100 2D array that looks like:

A1, B1, C1, D1
A2, B2, C2, D2
A3, B3, C3, D3
A4, B4, C4, D4

....

A100, B100, C100, D100

I tried using Convert a 1D array to a 2D array in numpy to reshape the array, but that doesn't solve the problem of turning my grouped array elements into separate array elements.

EDIT

My dataset was created from a normal Pandas dataframe with 6 columns.

df = pd.pivot_table(concord, index = ['Col1','Col2','Col3', 'Col4'], columns 
= 'Col5', values = 'Col6')

This produces an index comprised of 4 columns on a dataframe with as many columns as rows in Col5. I then produce my grouped array with.

df.index.values

Here is an example of what I am dealing with:

df = pd.DataFrame(np.random.randn(6,4),columns=list('ABCD'))
df = pd.pivot_table(df, index = ['A','B'], columns = 'C', values = 'D')
df.index.values
4
  • How exactly does your array look like when you print it? What you show is not a valid output from Python. It looks like your "grouped array elements" are tuples. You'd have to convert those into arrays first. Commented Dec 4, 2017 at 20:34
  • my array looks like array([('A', 'B', 318443.998, 398601.13),('C', 'D', 318443.998, 398601.13) ..... ]). I know it is an array because Python says its type is an array. It has strings inside it though if that is a concern. I got it by using the values function on the index of a pandas dataframe that combined four columns into a single index. Commented Dec 4, 2017 at 20:43
  • Python says it is a numpy.ndarray with shape of (100,) Commented Dec 4, 2017 at 20:44
  • Coming from pandas it is probably an object dtype array, one group per element. Commented Dec 4, 2017 at 21:17

1 Answer 1

1

UPDATE:

In [156]: df.index.values
Out[156]:
array([(-1.1589142652108702, 0.9058310711560317), (-0.8998389109349003, -1.3963768690741623), (-0.39909436307375407, 0.19524579552938015), (
0.3879397588199493, -1.6231489531567047),
       (0.4850794016257364, 0.9079649929281776), (1.263583531683934, -0.8985146123002069)], dtype=object)

In [157]: np.array(df.index.values.tolist())
Out[157]:
array([[-1.15891427,  0.90583107],
       [-0.89983891, -1.39637687],
       [-0.39909436,  0.1952458 ],
       [ 0.38793976, -1.62314895],
       [ 0.4850794 ,  0.90796499],
       [ 1.26358353, -0.89851461]])

Old answer:

I think you want to use Pandas:

In [89]: a
Out[89]:
array([['A', 'B', '318443.998', '398601.13'],
       ['C', 'D', '318443.998', '398601.13']],
      dtype='<U10')

In [90]: df = pd.DataFrame(a)

In [91]: df
Out[91]:
   0  1           2          3
0  A  B  318443.998  398601.13
1  C  D  318443.998  398601.13
Sign up to request clarification or add additional context in comments.

6 Comments

The array I have was originally produced in Pandas. Converting it into a data frame just gives me a dataframe with a single column where the elements are each grouping of items i want to separate, leaving me back where i started
@LeonardShaw, can you provide a reproducible data sample? When using a sample from your comment i was able to generate a DF of 2x4 shape
@LeonardShaw, I asked for a sample data set - not for an explanation how did you get it from the DF, which we can't see/access...
df = pd.DataFrame(np.random.randn(6,4),columns=list('ABCD')) df = pd.pivot_table(df, index = ['A','B'], columns = 'C', values = 'D') df.index.values
I added a sample data set
|

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.