Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.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 - Fatal编程技术网

Python 合并多个数据帧

Python 合并多个数据帧,python,pandas,Python,Pandas,我的数据如下: Name test1 test2 test3 Count Emp1 X,Y A a1,a2 1 Emp2 X A,B,C a3 2 Emp3 Z C a4,a5,a6 3 将具有多个值的test1和test2单元格拆分为单独的行,并将它们合并在一起 df2 = df.test1.str.split(',').apply(pd.Ser

我的数据如下:

Name    test1   test2    test3     Count
Emp1    X,Y      A       a1,a2      1
Emp2    X       A,B,C    a3         2
Emp3    Z        C       a4,a5,a6   3
将具有多个值的test1test2单元格拆分为单独的行,并将它们合并在一起

    df2 =  df.test1.str.split(',').apply(pd.Series)
    df2.index =  df.set_index(['Name', 'Count']).index
    df2=df2.stack().reset_index(['Name', 'Count'])
    df3 = df.test2.str.split(',').apply(pd.Series)
    df3.index = df.set_index(['Name', 'Count']).index
    df3=df3.stack().reset_index(['Name', 'Count'])

    df2.merge(df3,on=['Name', 'Count'],how='outer')
代码错误是:

Out[132]: 
   Name  Count 0_x 0_y
0  Emp1      1   X   A
1  Emp1      1   Y   A
2  Emp2      2   X   A
3  Emp2      2   X   B
4  Emp2      2   X   C
5  Emp3      3   Z   C
将具有多个值的Test3拆分到单个行的代码

    df4.index = df.set_index(['Name', 'Count']).index
    df4=df4.stack().reset_index(['Name', 'Count'])
有谁能帮助我,如何像我在上面的代码中合并了test1和test1一样,将Test3与test2和test1进行多连接

df1=df.stack().str.split(',').apply(pd.Series)
df1.stack().unstack(level=2).groupby(level=[0,1]).ffill().reset_index(level=[0,1])
Out[124]: 
   Name  Count test1 test2 test3
0  Emp1      1     X     A    a1
1  Emp1      1     Y     A    a2
0  Emp2      2     X     A    a3
1  Emp2      2     X     B    a3
2  Emp2      2     X     C    a3
0  Emp3      3     Z     C    a4
1  Emp3      3     Z     C    a5
2  Emp3      3     Z     C    a6
更像

df1=df.stack().str.split(',').apply(pd.Series)
df1.stack().unstack(level=2).groupby(level=[0,1]).ffill().reset_index(level=[0,1])
Out[124]: 
   Name  Count test1 test2 test3
0  Emp1      1     X     A    a1
1  Emp1      1     Y     A    a2
0  Emp2      2     X     A    a3
1  Emp2      2     X     B    a3
2  Emp2      2     X     C    a3
0  Emp3      3     Z     C    a4
1  Emp3      3     Z     C    a5
2  Emp3      3     Z     C    a6
(不确定我是否理解正确,但是)接下来,你可以

其中两个输出

    Name    test1   test2   Count
0   Emp1    X   A   1
1   Emp1    Y   A   1
2   Emp2    X   A   2
3   Emp2    X   B   2
4   Emp2    X   C   2
5   Emp3    Z   C   3

详情:

def expand(df, col, sep=','):
    r = df[col].str.split(sep)
    d = {c: df[c].values.repeat(r.str.len(), axis=0) for c in df.columns}
    d[col] = [i for sub in r for i in sub]
    return pd.DataFrame(d)
(不确定我是否理解正确,但是)接下来,你可以

其中两个输出

    Name    test1   test2   Count
0   Emp1    X   A   1
1   Emp1    Y   A   1
2   Emp2    X   A   2
3   Emp2    X   B   2
4   Emp2    X   C   2
5   Emp3    Z   C   3

详情:

def expand(df, col, sep=','):
    r = df[col].str.split(sep)
    d = {c: df[c].values.repeat(r.str.len(), axis=0) for c in df.columns}
    d[col] = [i for sub in r for i in sub]
    return pd.DataFrame(d)

我喜欢用理解力

pd.DataFrame([
    (T.Name, T.Count, t1, t2)
    for T in df.itertuples()
    for t1, t2 in product(T.test1.split(','), T.test2.split(','))
], columns=['Name', 'Count', '0_x', '0_y'])

   Name  Count 0_x 0_y
0  Emp1      1   X   A
1  Emp1      1   Y   A
2  Emp2      2   X   A
3  Emp2      2   X   B
4  Emp2      2   X   C
5  Emp3      3   Z   C

我喜欢用理解力

pd.DataFrame([
    (T.Name, T.Count, t1, t2)
    for T in df.itertuples()
    for t1, t2 in product(T.test1.split(','), T.test2.split(','))
], columns=['Name', 'Count', '0_x', '0_y'])

   Name  Count 0_x 0_y
0  Emp1      1   X   A
1  Emp1      1   Y   A
2  Emp2      2   X   A
3  Emp2      2   X   B
4  Emp2      2   X   C
5  Emp3      3   Z   C

你能解释一下你贴出的答案背后的逻辑吗?我有多个栏目,其中3个栏目需要拆分并与其他栏目合并columns@Mahesh首先将字符串列展平为单列,然后我们只需
unstack
您能解释一下您发布的答案背后的逻辑吗,我有多个列,其中3列需要拆分并与其他列连接columns@Mahesh首先将字符串列展平为单列,然后我们只需
unstack
Nice answer;)回答得好;)