pete
pete

Reputation: 601

Python dictionary to Pandas data frame with lists as values

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

Answers (4)

Tim
Tim

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

BENY
BENY

Reputation: 323376

In pandas 0.25.0

pd.Series(links).explode().reset_index()

Upvotes: 2

Allen Qin
Allen Qin

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

dtrckd
dtrckd

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

Related Questions