jenkelblankel
jenkelblankel

Reputation: 155

Making lists from a dict of lists

I have this dict:

{'Hours Outside Sprint': [5.25, 5.0, 0.0],
 'Sprint End': ['2017-02-14', '2017-02-14', '2017-02-14'],
 'Sprint Start': ['2017-01-31', '2017-01-31', '2017-01-31'],
 'Status': ['done', 'done', 'done'],
 'Story': ['SPGC-14075', 'SPGC-9456', 'SPGC-9445'],
 'Story Actual (Hrs)': [11.0, 12.75, 0.0],
 'Story Estimate (Hrs)': [16.0, 12.0, 0.0]}

I think this is a fairly simple task but the solution is not apparent at the moment. What I want to do is iterate through this dict and make the following:

[[done, 2017-02-14, SPGC-14075, 16.0, 5.25, 2017-01-31, 11.0], ... ]

So all the 1st elements of each list go together, all the 2nd, and so on until I have a list of lists. How do I do this?

EDIT:

Here is what the pandas dataframe looks liek that produced the above dict:

Story Status  Story Estimate (Hrs)  Story Actual (Hrs)  Hours Outside Sprint Sprint Start  Sprint End
0  SPGC-14075   done                  16.0               11.00                  5.25   2017-01-31  2017-02-14
1   SPGC-9456   done                  12.0               12.75                  5.00   2017-01-31  2017-02-14
2   SPGC-9445   done                   0.0                0.00                  0.00   2017-01-31  2017-02-14

Would iterrows work?

Upvotes: 2

Views: 91

Answers (5)

martineau
martineau

Reputation: 123443

Whenever you need to combine successive elements from two or more sequences in Python, think zip():

from pprint import pprint

data = {'Hours Outside Sprint': [5.25, 5.0, 0.0],
        'Sprint End': ['2017-02-14', '2017-02-14', '2017-02-14'],
        'Sprint Start': ['2017-01-31', '2017-01-31', '2017-01-31'],
        'Status': ['done', 'done', 'done'],
        'Story': ['SPGC-14075', 'SPGC-9456', 'SPGC-9445'],
        'Story Actual (Hrs)': [11.0, 12.75, 0.0],
        'Story Estimate (Hrs)': [16.0, 12.0, 0.0]}

# desired order of items in the result
key_order = ('Status', 'Sprint End', 'Story', 'Story Estimate (Hrs)',
             'Hours Outside Sprint', 'Sprint Start', 'Story Actual (Hrs)')
pprint([x[0] for x in zip(data[k] for k in key_order)])

Output:

[['done', 'done', 'done'],
 ['2017-02-14', '2017-02-14', '2017-02-14'],
 ['SPGC-14075', 'SPGC-9456', 'SPGC-9445'],
 [16.0, 12.0, 0.0],
 [5.25, 5.0, 0.0],
 ['2017-01-31', '2017-01-31', '2017-01-31'],
 [11.0, 12.75, 0.0]]

Upvotes: 1

Aaron
Aaron

Reputation: 11075

df.iterrows makes a pretty neat solution. Make sure to slice out the row index:
(i[0] = row_index; i[1] = row_values)

df = pd.DataFrame(df_dict)

#re-order columns (may not be necessary depending on your original df)
df = df[['Status','Sprint End','Story','Story Estimate (Hrs)','Hours Outside Sprint','Sprint Start','Story Actual (Hrs)']]
values = [i[1].tolist() for i in df.iterrows()]

Upvotes: 1

armnotstrong
armnotstrong

Reputation: 9065

map(lambda x: list(x),zip(*map(lambda (k,v): v, df_dict.iteritems())))

or

map(lambda x: list(x),zip(*df_dict.values()))

you could strip absolving method call one by one to see what you got each step

It's no more than transforming of your data.

*df_dict.values() means you could pass a list as parameters for a function that needs arguments likes this:

def fun(arg1, arg2, arg3 ...)

Upvotes: 0

Asher Dale
Asher Dale

Reputation: 149

Here's how I would do this in Python:

df_dict = {'Status': [u'done', u'done', u'done'], 'Sprint End': ['2017-02-14', '2017-02-14', '2017-02-14'], 'Story': [u'SPGC-14075', u'SPGC-9456', u'SPGC-9445'], 'Story Estimate (Hrs)': [16.0, 12.0, 0.0], 'Hours Outside Sprint': [5.25, 5.0, 0.0], 'Sprint Start': ['2017-01-31', '2017-01-31', '2017-01-31'], 'Story Actual (Hrs)': [11.0, 12.75, 0.0]}

result = []
lengthOfFirstArrInDict = len(df_dict[df_dict.keys()[0]])

for i in range(0, lengthOfFirstArrInDict):
    nestedList = []

    for key in df_dict.keys():
        nestedList.append(df_dict[key][i])

    result.append(nestedList)

print(result)

And here's the output:

[['done', '2017-02-14', 'SPGC-14075', 16.0, 5.25, '2017-01-31', 11.0], ['done', '2017-02-14', 'SPGC-9456', 12.0, 5.0, '2017-01-31', 12.75], ['done', '2017-02-14', 'SPGC-9445', 0.0, 0.0, '2017-01-31', 0.0]]

Upvotes: 1

Ajax1234
Ajax1234

Reputation: 71451

You can try this:

df_dict = {'Status': [u'done', u'done', u'done'], 'Sprint End': ['2017-02-14', '2017-02-14', '2017-02-14'], 'Story': [u'SPGC-14075', u'SPGC-9456', u'SPGC-9445'], 'Story Estimate (Hrs)': [16.0, 12.0, 0.0], 'Hours Outside Sprint': [5.25, 5.0, 0.0], 'Sprint Start': ['2017-01-31', '2017-01-31', '2017-01-31'], 'Story Actual (Hrs)': [11.0, 12.75, 0.0]}

vals = df_dict.values()

final_data = list(map(list, zip(*vals)))

print(final_data)

Output:

[[16.0, 5.25, 11.0, '2017-02-14', 'done', 'SPGC-14075', '2017-01-31'], [12.0, 5.0, 12.75, '2017-02-14', 'done', 'SPGC-9456', '2017-01-31'], [0.0, 0.0, 0.0, '2017-02-14', 'done', 'SPGC-9445', '2017-01-31']]

Upvotes: 0

Related Questions