I have some data that looks a little like this:
data=[([('thing1',
'thing1a'),
('thing1',
'thing1b'),
('thing1',
'thing1c'),
('thing1',
'thing1d'),
('thing1',
'thing1e')],
'thing1description'),
([('thing2',
'thing2a')],
'thing2description'),
([('thing3',
'thing3a')],
'thing3description')]
I would like to build a dataframe that looks like this:
thing_number thing_letter description
thing1 thing1a thing1description
thing1 thing1b thing1description
thing1 thing1c thing1description
thing1 thing1d thing1description
thing1 thing1e thing1description
thing2 thing2a thing2description
thing3 thing3a thing3description
thanks to a previous very similar question such as this I can achieve it using the below but I think I must be missing something to make this more elegant:
data_=pd.DataFrame(data,columns=['thing','description'])
data_=data_.explode('thing')
data_=pd.concat([data_,pd.DataFrame([(*i, k) for k,j in data for i in k], columns=['thing_number','thing_letter','all'],index=data_.index)],axis=1)
data_=data_[['thing_number','thing_letter','description']]
To summarise I am looking for a more efficient and elegant way to unnest the list of tuples. Thanks in advance.