Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.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 数据帧中3列的所有可能组合_Python_Pandas_Itertools - Fatal编程技术网

Python 数据帧中3列的所有可能组合

Python 数据帧中3列的所有可能组合,python,pandas,itertools,Python,Pandas,Itertools,这是对前面问题的跟进 我试图获取一个数据帧并创建另一个数据帧,将3列的所有可能组合放在一起,并计算相应值之间的差值,即在4月11日,列ABC应该是(2B-A-C)=0,然后是2*B-A-D=0,依此类推 e、 g,从 Dt A B C D 11-apr 1 1 1 1 10-apr

这是对前面问题的跟进

我试图获取一个数据帧并创建另一个数据帧,将3列的所有可能组合放在一起,并计算相应值之间的差值,即在4月11日,列ABC应该是(2B-A-C)=0,然后是2*B-A-D=0,依此类推

e、 g,从

        Dt              A           B           C          D
        11-apr          1           1           1          1
        10-apr          2           3           1          2
如何获得如下所示的新框架:

我认为需要:

cc = list(combinations(df.columns,3))

df = pd.concat([df[c[1]].mul(2).sub(df[c[2]]).sub(df[c[0]]) for c in cc], axis=1, keys=cc)
df.columns = df.columns.map(''.join)
print (df)
        ABC  ABD  ACD  BCD
Dt                        
11-apr    0    0    0    0
10-apr    3    2   -2   -3
我认为需要:

cc = list(combinations(df.columns,3))

df = pd.concat([df[c[1]].mul(2).sub(df[c[2]]).sub(df[c[0]]) for c in cc], axis=1, keys=cc)
df.columns = df.columns.map(''.join)
print (df)
        ABC  ABD  ACD  BCD
Dt                        
11-apr    0    0    0    0
10-apr    3    2   -2   -3

这里使用的方法与jezrael在您的中提供的答案相同-您只需将
concat
部分转换为以下内容:
df=pd.concat([(2*df[c[1]]).sub(对于cc中的c,df[c[0]]+df[c[2]]),axis=1,keys=cc)
这里应用的方法与jezrael在您的-中提供的答案相同,您只需将
concat
部分转到以下内容:
df=pd.concat([(2*df[c[1]]).sub(df[c[0]]+df[c[2]]),对于cc中的c],axis=1,keys=cc)