0

I've google this a bit but notice the answers always lie with numpy/panda etc. I would like to do this without any imports. I'm just learning how to use Python so apologies if I label things incorrectly.

I have a data set which has been opened and read and is like this:

['Label 1', 41.0, 34.2, 97.0, 52.0, None, None, 68.0, 58.0]
['Label 2', None, 78.0, 62.0, 75.0, None, 67.0, None, None]
['Label 3', 51.0, None, 68.0, 51.0, 66.0, None, 55.0, 72.0]
['Label 4', None, 54.0, 47.0, 59.0, None, 48.0, None, None]

All of this data I can pull from 'data_sample'

When I type in data_sample[1]. It will give me:

41.0

None
51.0
None

I want to be able to have the data that's in the 'rows' be in it's own list.

Label 1 = ['Label 1', 41.0, 34.2, 97.0, 52.0, None, None, 68.0, 58.0]

I can't specifically call only value 51 from Label 3. I can call the whole column of data, but i can't call that specific value of 51. If that helps?

Is it possible?

I tried:

for n in students_file:
    if "Label 1" in n:
        print(n)
May1234
  • 1
  • 1
  • Could you please explain clearly? sample input and output would be very helpful – Shivam Singh Apr 26 '18 at 05:15
  • `1` and what's the problem (current output) of your code? `2` We format code with 4-space indentation (Ctrl+K). See https://meta.stackoverflow.com/questions/251361/how-do-i-format-my-code-blocks – user202729 Apr 26 '18 at 05:16
  • 1
    [Possible dupliate](https://stackoverflow.com/questions/17037566/transpose-a-matrix-in-python). – user202729 Apr 26 '18 at 05:18
  • I can't specifically call only value 51 from Label 3. I can call the whole column of data, but i can't call that specific value of 51. If that helps? – May1234 Apr 26 '18 at 05:21

2 Answers2

0

You can construct dictionary for the same as below:

In [3]: data
Out[3]: 
[['Label 1', 41.0, 34.2, 97.0, 52.0, None, None, 68.0, 58.0],
 ['Label 2', None, 78.0, 62.0, 75.0, None, 67.0, None, None],
 ['Label 3', 51.0, None, 68.0, 51.0, 66.0, None, 55.0, 72.0],
 ['Label 4', None, 54.0, 47.0, 59.0, None, 48.0, None, None]]

In [4]: {x[0]:x[1:] for x in data}
Out[4]: 
{'Label 1': [41.0, 34.2, 97.0, 52.0, None, None, 68.0, 58.0],
 'Label 2': [None, 78.0, 62.0, 75.0, None, 67.0, None, None],
 'Label 3': [51.0, None, 68.0, 51.0, 66.0, None, 55.0, 72.0],
 'Label 4': [None, 54.0, 47.0, 59.0, None, 48.0, None, None]}

as you can see above your label is stored as key in dictionary and the values are stored in corresponding keys as list

you can store the dict in a variable say data_set

and you can access values like:

`data_set['Label 1']`  # [41.0, 34.2, 97.0, 52.0, None, None, 68.0, 58.0]
Gahan
  • 4,075
  • 4
  • 24
  • 44
0

If data_sample[1] is producing the output you say (41.0, None, 51.0, None) then this shows that your data has been read in as columns, not rows. I can't tell from your question what the exact structure of data_sample is, but let's assume it's a 2-dimensional array, arranged column-wise, like this:

# your existing data, in columns
data_sample= [
    ['Label 1','Label 2','Label 3','Label 4'],
    [41.0, None, 51.0, None],
    [34.2, 78.0, None, 54.0],
    [97.0, 62.0, 68.0,     47.0],
    [52.0, 75.0, 51.0, 59.0],
    [None, None, 66.0, None],
    [None, 67.0, None, 48.0],
    [68.0, None, 55.0, None],
    [58.0, None, 72.0, None]
]

Rather than transposing it, you can just define some simple functions to access the information by row:

def getRowIndexFromName(row_name):
    return data_sample[0].index(row_name)

def getRowByName(row_name):
    r = getRowIndexFromName(row_name)
    return [x[r] for x in data_sample[1:]]

# now you can access an entire row by name:
row_3 = getRowByName('Label 3')
print(row_3)
# [51.0, None, 68.0, 51.0, 66.0, None, 55.0, 72.0]

# and you can get an individual item in that row, by index number:
print(row_3[0])
# 51.0
Richard Inglis
  • 5,888
  • 2
  • 33
  • 37