Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 865
krtkush Avatar asked Jan 25 '23 14:01

krtkush


2 Answers

Use:

pd.Series(dct, name='model').explode().rename_axis(index='vehicle').reset_index()
like image 90
ansev Avatar answered May 22 '23 14:05

ansev


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
like image 43
Erfan Avatar answered May 22 '23 14:05

Erfan