0

I am trying to create a number of new arrays with unique naming. The name of each array is defined by the information in a pandas dataframe.

import pandas as pd 
import numpy as np 

d = {'ColA':[152689, 221330, 245521], 'ColB':[005263, 135255, 102120], 'ColC':[132565, 187514, 414141]}
Sample = pd.DataFrame(data=d)
SampleForNames = Sample[['ColA', 'ColB']].copy() 

What I want are new (empty) arrays called '152689_005263', '221330_135255', '245521_102120' and so on. The total number of new arrays will be the number of rows there are in the dataframe.

Here is what I am thinking of writing, as pseudocode.

1// Information from above 

2// for i in range(len(Sample['ColA'])):
3//       FirstName = Sample['ColA'][i]
4//       SecondName = Sample['ColB'][i]
5//       TotalName = 'Loc' + str(FirstName) + '_' + str(SecondName)
6//       TotalName = []

I don't think line 6 works... Any advice?

Erika H.
  • 1
  • 1
  • What information do you want stored in the array? – It_is_Chris Jan 19 '23 at 18:23
  • I'm going to populate it later, but nothing needs to be stored right now. I just want to be able to generate arrays with unique names. – Erika H. Jan 19 '23 at 18:24
  • By 'it doesn't work', I find that by running through the code above, I have an array called TotalName (with nothing in it), but no arrays with the name '152689_005263', '221330_135255', or '245521_102120' exist. That's what I want to have exist. – Erika H. Jan 19 '23 at 18:25
  • Variable names can't start with numbers. Also, don't do this. Use a dict instead. – MattDMo Jan 19 '23 at 18:25
  • 1
    That is not quite how things work but you can store them as keys of a dictionary and populate that later. – It_is_Chris Jan 19 '23 at 18:25
  • Thanks @MattDMo I didn't know that. I know arrays can be multidimensional (which is what will happen if I eventually populate these arrays), but can dictionaries be 'multidimensional'? – Erika H. Jan 19 '23 at 18:34
  • First, a note about nomenclature: the term for what you're calling an "array" is `list` in Python. I'm not quite sure what you mean by multidimensional, but if it means what I'm guessing it means, then the answer is yes. A dictionary value can be just about anything, including a multilevel list, another dict, a dict of lists, a dict of dicts containing yet more dicts... you can nest as much as you need to. – MattDMo Jan 19 '23 at 18:40
  • Also, if you're going to be doing intensive numerical computations, you may actually want to consider the NumPy array (`np.ndarray`) instead of lists of lists. – MattDMo Jan 19 '23 at 18:41
  • What do you mean by "names"? Do you mean *variables*? Don't do that. You should use a *container* like a `list` or a `dict` to hold your arrays. Don't dynamically create variables. – juanpa.arrivillaga Jan 19 '23 at 18:42
  • @MattDMo I think in this case they will actually turn out to be `np.ndarray` objects because they are being used to construct a `pd.DataFrame` – juanpa.arrivillaga Jan 19 '23 at 18:43

0 Answers0