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?
Use:
pd.Series(dct, name='model').explode().rename_axis(index='vehicle').reset_index()
                        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
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With