将元组从pandas DataFrame中的众多列转换为行 [英] Converting tuples into rows from numerous columns in a pandas DataFrame

查看:365
本文介绍了将元组从pandas DataFrame中的众多列转换为行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的字典:

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')]}

我想将其转换为pandas DataFrame,并使它看起来像这样:

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?

编辑:

批准的解决方案似乎有点问题.也就是说,如果存在一个完全由None组成的倒置的列,或者只是空的元组,那么在形成new_frame的过程中,它们将被丢弃.是否有可能使大熊猫避免掉落柱子.

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.

初始数据如下:

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.

推荐答案

由于要扩展多个列,我不认为这可以在单行中显示,但可以将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

希望有帮助.

这篇关于将元组从pandas DataFrame中的众多列转换为行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆