简体   繁体   中英

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?

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

用:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM