Reputation: 2061
I have several objects from type dict
with different keys. I want to create a table with all keys
and foreach object one row. If one key isn't available, it should be empty.
For Example:
x1=dict( {"a":2, "b":3})
x2=dict( {"a":2, "b":3, "c":2})
and i want to get something like this:
"a","b","c"
2,3,
2,3,2
Upvotes: 0
Views: 970
Reputation: 4656
Here is a very crude and possibly inefficient solution
x1=dict( {"a":2, "b":3,"d":4,"e":5})
x2=dict( {"a":2, "b":3, "c":2})
z = dict(x1.items() + x2.items())
print(z.keys())
x1_vals = []
x2_vals = []
for keys in z.keys():
if keys in x1.keys():
x1_vals.append( x1[keys])
else:
x1_vals.append(None)
if keys in x2.keys():
x2_vals.append(x2[keys])
else:
x2_vals.append(None)
print (x1_vals)
print (x2_vals)
Output
['a', 'c', 'b', 'e', 'd']
[2, None, 3, 5, 4]
[2, 2, 3, None, None]
Upvotes: 0
Reputation: 15433
If you use pandas, you can do this:
import pandas as pd
df = pd.DataFrame({k: [v] for k, v in x1.iteritems()})
df2 = pd.DataFrame({k: [v] for k, v in x2.iteritems()})
df = pd.concat((df, df2), ignore_index=True)
# a b c
# 0 2 3 NaN
# 1 2 3 2
Note: iteritems()
only works in Python 2.x.
Upvotes: 1
Reputation: 15433
Here is another simple solution that does not use pandas
:
all_dics = [x1, x2]
keys = set(key for d in all_dics for key in d) # {'a', 'b', 'c'}
dic = {key: [None]*len(all_dics) for key in keys} # {'a': [None, None], 'b': [None, None], 'c': [None, None]}
for j, d in enumerate(all_dics):
for key, val in d.iteritems():
dic[key][j] = val
print dic
# {'a': [2, 2], 'b': [3, 3], 'c': [None, 2]}
Upvotes: 0
Reputation: 21288
As a general approach (I am assuming you have a list of dicts, here, and you are fine with having the columns in "asciibetical" order):
def EmitDictsAsCSV(list_of_dicts):
# First, accumulate the full set of dict keys
key_set = set()
for d in list_of_dicts:
key_set.update(d.iterkeys())
# make a sorted list out of them
column_names = sorted(key_set)
# print the header
print ",".join(['"%s"' % c for c in column_names])
# For each dict, loop over the columns and build a row,
# use the string representation of the value, if there's
# one, otherwise use an empty string,
# finish off by printing the row data, separated by commas
for d in list_of_dicts:
row_data = []
for c in column_names:
if c in d:
row_data.append(str(d[c]))
else:
row_data.append("")
print ",".join(row_data)
Upvotes: 0