Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 熊猫由两个相似的列和两个不同的列组成_Python_Pandas_Dataframe - Fatal编程技术网

Python 熊猫由两个相似的列和两个不同的列组成

Python 熊猫由两个相似的列和两个不同的列组成,python,pandas,dataframe,Python,Pandas,Dataframe,我有这样的数据帧: ID | Name | Thing | belongs | match ---+------+-------+---------+----- 1 John 10 1,2,3 9 2 John 10 2,4 8 输出应为: John 10 1,2,3,2,4 9,9,9,8,8 我如何将它们分组 def f(df): lol = df.belongs.str.split(

我有这样的数据帧:

    ID | Name | Thing | belongs | match
    ---+------+-------+---------+-----
     1   John     10     1,2,3     9
     2   John     10      2,4      8
输出应为:

John 10 1,2,3,2,4 9,9,9,8,8
我如何将它们分组

def f(df):
    lol = df.belongs.str.split(',').tolist()
    lens = [len(lst) for lst in lol]
    belongs = ','.join(map(str, np.concatenate(lol)))
    match = ','.join(map(str, df.match.repeat(lens).tolist()))

    return pd.Series(dict(
            belongs=belongs,
            match=match
        ))

df.groupby(['Name', 'Thing']).apply(f).reset_index()

   Name  Thing    belongs      match
0  John     10  1,2,3,2,4  9,9,9,8,8

略为不同的方法。确定差异是留给读者的任务

def f(df):
    lens = df.belongs.str.count(',') + 1
    belongs = df.belongs.str.cat(sep=',')
    match = df.match.repeat(lens).map(str).str.cat(sep=',')

    return pd.Series(dict(
            belongs=belongs,
            match=match
        ))

print(df.groupby(['Name', 'Thing']).apply(f).reset_index())

   Name  Thing    belongs      match
0  John     10  1,2,3,2,4  9,9,9,8,8

“match”字符串出现错误-无法根据规则“safe”将数组数据从dtype('int64')转换为dtype('int32')。是否可以以某种方式解决此问题?