0

I have multiple text files with reaction time results in them for different colours. I have extracted the ones i want to work with and found out the standard deviation and mean for these results. They are in multiple dictionaries within a larger dictionary. Next i am wanting to put them into a table of results. So far i can only get it to produce 3 columns and i need to add another 2 columns for SD and number of samples. without using pandas

this is the code i have so far, could someone please assist in how to add another 2 columns

   data = [(k,*x)  for k, v in res.items() for x in v.items()]
print ("ROI            COLOUR        MEAN                  SD")
for l in data:
        print ("{:<14}{:<11}{}".format(l[0],l[1],l[2]))

My output from this code currently looks like this, where the ffam, lingualm and ppam are the means but then the SD is underneath instead of going under the SD part.

ROI           COLOUR        MEAN                  SD
ffam          cope1      0.6525
ffam          cope2      0.4146
ffam          cope3      0.5896
ffam          cope4      0.1521
ffam          cope5      0.5317
lingualm      cope1      -0.08865060000000001
lingualm      cope2      -0.150985
lingualm      cope3      -0.162005
lingualm      cope4      -0.130845
lingualm      cope5      -0.126411
ppam          cope1      0.74836
ppam          cope2      0.9444
ppam          cope3      0.300482
ppam          cope4      1.12435
ppam          cope5      0.8332200000000001
ffa           cope1      0.0
ffa           cope2      0.0
ffa           cope3      0.0
ffa           cope4      0.0
ffa           cope5      0.0
lingual       cope1      0.18218909587360052
lingual       cope2      0.19045722970000376
lingual       cope3      0.156594265045052
lingual       cope4      0.1723191040627823
lingual       cope5      0.1327233022080147
ppa           cope1      0.46169785834461047
ppa           cope2      0.43539771473906475
ppa           cope3      0.25656049991376306
ppa           cope4      0.40710618332322096
ppa           cope5      0.5046582384148702
codebbe
  • 13
  • 4

1 Answers1

0

So you can create your format string, then just unpack the values of your tuples as you iterate over the data.

data = [('ffam', 'cope1', 0.6525, 10, 100), ('ffam', 'cope2', 0.4146, 45, 12), ('ffam', 'cope3', 0.5896, 100, 15)]
headers = ('ROI', 'COLOUR', 'MEAN', 'SD', 'SAMPLES')
string_format = "{:<14}{:<11}{:<10}{:<5}{:<5}"
for line in [headers, *data]:
    print(string_format.format(*line))

OUTPUT

ROI           COLOUR     MEAN      SD   SAMPLES
ffam          cope1      0.6525    10   100  
ffam          cope2      0.4146    45   12   
ffam          cope3      0.5896    100  15 
Chris Doyle
  • 10,703
  • 2
  • 23
  • 42
  • Is there a way to do it without having the data in the script like you have? and instead to pull it from the tuples in the dictionary? – codebbe Dec 17 '19 at 23:10
  • of course, i only wrote data like this as i assumed you could make your data variable eith tuples of 5 items. If you cant then you need to update the question with an example of your dictionary and its format and say which parts you want – Chris Doyle Dec 17 '19 at 23:12
  • sorry python newbie here, I'm not sure how to get the full dictionary on here. But basically in the dictionary there are 6 keys (3 means, 3 SD) each the size of 5 which are the 5 copes. for example {'cope1': 0.6525, 'cope2': 0.4146, 'cope3': 0.5896, 'cope4': 0.1521, 'cope5': 0.5317}. I know its not the best layout as the mean and SD are separate, but I don't know how to combine them – codebbe Dec 17 '19 at 23:37