简体   繁体   中英

Appending to a list from an iterated dictionary

I've written a piece of script that at the moment I'm sure can be condensed. What I'm try to achieve is an automatic version of this:

file1 = tkFileDialog.askopenfilename(title='Select the first data file')
file2 = tkFileDialog.askopenfilename(title='Select the first data file')
TurnDatabase = tkFileDialog.askopenfilename(title='Select the turn database file')

headers = pd.read_csv(file1, nrows=1).columns
data1 = pd.read_csv(file1)
data2 = pd.read_csv(file2)

This is how the data is collected. There's many more lines of code which focus on picking out bits of the data. I'm not going to post it all.

This is what I'm trying to condense:

EntrySummary = []
for key in Entries1.viewkeys():
    MeanFRH = Entries1[key].hRideF.mean()
    MeanFRHC = Entries1[key].hRideFCalc.mean()
    MeanRRH = Entries1[key].hRideR.mean()
# There's 30 more lines of these...
# Then the list is updated  with this:
EntrySummary.append({'Turn Number': key, 'Avg FRH': MeanFRH, 'Avg FRHC': MeanFRHC, 'Avg RRH': MeanRRH,... # and so on})


EntrySummary = pd.DataFrame(EntrySummary)
EntrySummary.index = EntrySummary['Turn Number']
del EntrySummary['Turn Number']

This is the old code. What I've tried to do is this:

EntrySummary = []

for i in headers():
    EntrySummary.append({'Turn Number': key, str('Avg '[i]): str('Mean'[i])})
    print EntrySummary
# The print is only there for me to see if it's worked. 

However I'm getting this error at the minute:

for i in headers():
TypeError: 'Index' object is not callable

Any ideas as to where I've made a mistake? I've probably made a few...

Thank you in advance

Oli

If I'm understanding your situation correctly, you want to replace the long series of assignments in the "old code" you've shown with another loop that processes all of the different items automatically using the list of headers from your data files.

I think this is what you want:

EntrySummary = []
for key, value in Entries1.viewitems():
    entry = {"Turn Number": key}
    for header in headers:
        entry["Avg {}".format(header)] = getattr(value, header).mean()
    EntrySummary.append(entry)

You might be able to come up with some better variables names, since you know what the keys and values in Entries1 are (I do not, so I used generic names).

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