简体   繁体   中英

Accessing same value for all elements in dictionary of tuples

I have a dictionary containing keys all with a 7-tuple as their values. For every 7-tuple, each of the 7 values represents a different property corresponding to the data set that key is representing. For example, the first element of each tuple is an array of that data sets raw data, the second value is the mean of the data, the third value is the error on that mean, the fourth value is the temperature at which that data set was taken etc. I would like to plot various different combinations of these values, for example, the mean value vs the temperature.

So I need to find a way to convert the dictionary values into different lists containing values all of the same property ie a list of all of the mean values and a list of all of the temperatures. I understand that dictionaries are inherently unordered which complicates matters as each value in the lists would need to have the same index as its corresponding value in the other list.

dictionary={}
for i in range(len(imagefolders)):
   dictionary[slugify(imagefolders[i])]=images2(r{}\*.png".format(imagefolders[i]))

plotting=dictionary.values()

The above code shows how I inputted the keys and values to the dictionary. I know that dictioanry.values() returns a list of all the values in the dictionary however when I tried the above code it didn't even store this as a variable.

I am thinking now maybe using a dictionary to store this data wasn't the best option so if anyone has any better ideas of doing that then please do say but a method of making it work with a dictionary would be ideal.

I'm fairly inexperienced with python so would appreciate any contribution.

I would recommend you take a look into pandas . It's made for data analysis and has a ton of super useful functions. It might have a bit of a curve to it, but there's pletnty of documentation here on stackoverflow.

To show you some of the power, I created a mock dict similar to what you described. I am using a very simple tuple to help demonstrate the position of the elements for later

columns = ['data', 'mean', 'error', 'temperature', 'other1', 'other2', 'other3']
d = {}
for i in ['one', 'two', 'three', 'four', 'five']:
    d[i] = (1, 2, 3, 4, 5, 6, 7)

print(d)
# {
#   'one':   (1, 2, 3, 4, 5, 6, 7),
#   'two':   (1, 2, 3, 4, 5, 6, 7),
#   'three': (1, 2, 3, 4, 5, 6, 7),
#   'four':  (1, 2, 3, 4, 5, 6, 7),
#   'five':  (1, 2, 3, 4, 5, 6, 7)
# }

If the structrue above matches the structure of your dict, then we're in good shape. To put it all into one giant table, all you need are the lines below to make all the magic accessible.

import pandas as pd
df = pd.DataFrame.from_dict(d, orient='index', columns=columns)
print(df)
#        data  mean  error  temperature  other1  other2  other3
# three     1     2      3            4       5       6       7
# one       1     2      3            4       5       6       7
# five      1     2      3            4       5       6       7
# two       1     2      3            4       5       6       7
# four      1     2      3            4       5       6       7

To access a given field, you can index it based on the column and then perform statistics on it if you'd like.

print(df['data'])
# one      1
# three    1
# four     1
# two      1
# five     1
# Name: data, dtype: int64

print(df['data'].mean())
# 1.0

Pandas also has built in support for matplotlib via the df.plot() function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM