更新时间:2021-07-14 18:44:43
由于要扩展多个列,我不认为这可以在单行中显示,但可以将apply与pd.DataFrame构造函数一起使用. dropna堆栈方法的默认值为True,因此将其设置为false以保留None值.即
Since there are multiple columns to expand I dont think this can be in single line but you can use apply with pd.DataFrame constructor. The default value of dropna for stack method is True so set it to false to keep the None values. i.e
index = ['function_name','A','B']
new_frame = data_frame.set_index(index)
.apply(lambda x:pd.DataFrame(x.values.tolist()).stack(dropna=False),1)
.stack(dropna=False).reset_index().drop('level_3',1)
new_frame.columns = index + [x for x in data_frame.columns if x not in index]
function_name A B argument types
0 func1 value_a1 b func1_arg1 func1_type1
1 func1 value_a1 b func1_arg2 func1_type2
2 func2 value_a2 b func2_arg1 func2_type1
3 func3 value_a3 b func3_arg1 func3_type1
4 func3 value_a3 b func3_arg2 func3_type2
5 func3 value_a3 b func3_arg3 func3_type3
要扩展三列
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)]}
function_name A B argument info types
0 func1 value_a1 b func1_arg1 None func1_type1
1 func1 value_a1 b func1_arg2 None func1_type2
2 func2 value_a2 b func2_arg1 None func2_type1
3 func3 value_a3 b func3_arg1 None func3_type1
4 func3 value_a3 b func3_arg2 None func3_type2
5 func3 value_a3 b func3_arg3 None func3_type3
希望有帮助.