krtkush
krtkush

Reputation: 1528

Create dataframe from dictionary where arrays are of unequal length

I have a dictionary - {'Car': ['a', 'b'], 'Bike': ['q', 'w', 'e']}

I want to generate a data frame like this -

S.no. | vehicle | model
1     | Car     | a
2     | Car     | b
2     | Bike     | q
2     | Bike     | w
2     | Bike     | e

I tried df = pd.DataFrame(vDict) but I get ValueError: arrays must all be same length error. Help please?

Upvotes: 4

Views: 184

Answers (2)

Erfan
Erfan

Reputation: 42896

We can use pd.DataFrame.from_dict here, then use stack and finally clean up our index and column names:

dct = {'Car': ['a', 'b'], 'Bike': ['q', 'w', 'e']}

df = pd.DataFrame.from_dict(dct, orient='index').stack()
df = df.reset_index(level=0, name='model').rename(columns={'level_0':'vehicle'})
df = df.reset_index(drop=True)
  vehicle model
0     Car     a
1     Car     b
2    Bike     q
3    Bike     w
4    Bike     e

Upvotes: 4

ansev
ansev

Reputation: 30920

Use:

pd.Series(dct, name='model').explode().rename_axis(index='vehicle').reset_index()

Upvotes: 4

Related Questions