Reputation: 601
I want to convert the following dictionary:
links = { "a": ["aa", "ab", "ac"], "b": ["ba", "bb", "bc", "bd"] }
to a Pandas data frame like:
col_a col_b
a aa
a ab
a ac
b ba
b bb
b bc
b bd
which is a little bit different than the classic dictionary to Pandas examples. Any ideas?
Upvotes: 1
Views: 260
Reputation: 2049
Use a list comprehension to create a list of (col_a, col_b)
tuples, then use DataFrame.from_records()
:
records = [(k, i) for k, v in links.items() for i in v]
df = pd.DataFrame.from_records(records, columns=["col_a", "col_b"])
Upvotes: 1
Reputation: 19957
You can first create a df using from_dict then transpose it and re-arrange and rename:
(
pd.DataFrame.from_dict(links, orient='index')
.T.stack()
.reset_index(level=0, drop=True)
.sort_index()
.reset_index()
.set_axis(['col_a','col_b'], 1, inplace=False)
)
col_a col_b
0 a aa
1 a ab
2 a ac
3 b ba
4 b bb
5 b bc
6 b bd
Upvotes: 0
Reputation: 662
You can go like this with list comprehension and native python operators:
d = [([k]*len(v), v) for k,v in links.items()]
d2 = list(map(lambda x:sum(x, []), zip(*d)))
df = pd.DataFrame(d2).T
df.columns = ['col_a', 'col_b']
Upvotes: 0