I've got a dictionary that look like this:
data = {'function_name': ['func1', 'func2', 'func3'],
'argument': [('func1_arg1', 'func1_arg2'),
('func2_arg1',),
('func3_arg1', 'func3_arg2', 'func3_arg3')],
'A': ['value_a1', 'value_a2', 'value_a3'],
'B': 'b',
'types': [('func1_type1', 'func1_type2'),
('func2_type1',),
('func3_type1', 'func3_type2', 'func3_type3')]}
I'd like to convert it into a pandas DataFrame and make it look like this:
function_name argument types A B
func1 func1_arg1 func1_type1 value_a1 b
func1 func1_arg2 func1_type2 value_a1 b
func2 func2_arg1 func2_type1 value_a2 b
func3 func3_arg1 func3_type1 value_a3 b
func3 func3_arg2 func3_type2 value_a3 b
func3 func3_arg3 func3_type3 value_a3 b
As it follows from here if there would be one column of tuples, I would have to do this:
import pandas as pd
data_frame = pd.DataFrame(data)
new_frame = data_frame.set_index(['function_name','A','B'])['argument'].apply(pd.Series).stack().to_frame('argument').reset_index().drop('level_3',1)
But how do I go about it if I've got a few columns of tupples?
EDIT:
There seems to be a little problem with the approved solution. Namely, if there's a tuppled column consisting entirely of Nones or just empty tuples then in the process of forming the new_frame they get dropped. Is it possible to make pandas avoid dropping the columns.
The initial data looks like this:
data = {'function_name': ['func1', 'func2', 'func3'],
'argument': [('func1_arg1', 'func1_arg2'),
('func2_arg1',),
('func3_arg1', 'func3_arg2', 'func3_arg3')],
'A': ['value_a1', 'value_a2', 'value_a3'],
'B': 'b',
'types': [('func1_type1', 'func1_type2'),
('func2_type1',),
('func3_type1', 'func3_type2', 'func3_type3')],
'info': [(None, None), (None,), (None, None, None)]}
The 'info' columns could be [(), (), ()], the outcome would still be the same.