0

I want to convert the below pandas data frame

data = pd.DataFrame([[1,2], [5,6]], columns=['10+', '20+'], index=['A', 'B'])
data.index.name = 'City'
data.columns.name= 'Age Group'
print data

Age Group  10+  20+
City               
A            1    2
B            5    6

in to an array of dictionaries, like

[
 {'Age Group': '10+', 'City': 'A', 'count': 1},
 {'Age Group': '20+', 'City': 'A', 'count': 2},
 {'Age Group': '10+', 'City': 'B', 'count': 5},
 {'Age Group': '20+', 'City': 'B', 'count': 6}
]

I am able to get the above expected result using the following loops

result = []
cols_name = data.columns.name
index_names = data.index.name
for index in data.index:
    for col in data.columns:
        result.append({cols_name: col, index_names: index, 'count': data.loc[index, col]})

Is there any better ways of doing this? Since my original data will be having large number of records, using for loops will take more time.

1 Answer 1

1

I think you can use stack with reset_index for reshape and last to_dict:

print (data.stack().reset_index(name='count'))
  City Age Group  count
0    A       10+      1
1    A       20+      2
2    B       10+      5
3    B       20+      6

print (data.stack().reset_index(name='count').to_dict(orient='records'))
[
  {'Age Group': '10+', 'City': 'A', 'count': 1}, 
  {'Age Group': '20+', 'City': 'A', 'count': 2}, 
  {'Age Group': '10+', 'City': 'B', 'count': 5}, 
  {'Age Group': '20+', 'City': 'B', 'count': 6}
]
Sign up to request clarification or add additional context in comments.

1 Comment

Sure, no problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.