Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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_Arrays_Python 3.x_Pandas - Fatal编程技术网

Python 两列交互

Python 两列交互,python,arrays,python-3.x,pandas,Python,Arrays,Python 3.x,Pandas,我想利用现有列的成对交互来制作列。下面的代码返回所有可能的交互(双向、三向等),而我只需要一对列。任何关于如何使其工作的想法都将不胜感激 import pandas as pd for i in range(0, df.columns.size): for j in range (i + 1, df.columns.size): col1 = str(df.columns[i]) col2 = str(df.columns[j]) nam

我想利用现有列的成对交互来制作列。下面的代码返回所有可能的交互(双向、三向等),而我只需要一对列。任何关于如何使其工作的想法都将不胜感激

import pandas as pd

for i in range(0, df.columns.size):
    for j in range (i + 1, df.columns.size):
        col1 = str(df.columns[i])
        col2 = str(df.columns[j])
        nam = col1 + "X" + col2
        df[nam] = pd.Series(df[col1] * df[col2], name=nam)

这有点棘手,但以下情况正在发生,df.columns.size在循环中发生变化,因为您正在创建新列,您可以在循环外创建size\u col,这样就不会每次都计算它:

import pandas as pd

size_col = df.columns.size
for i in range(0, size_col):
    for j in range (i + 1, size_col):
        col1 = str(df.columns[i])
        col2 = str(df.columns[j])
        nam = col1 + "X" + col2
        df[nam] = pd.Series(df[col1] * df[col2], name=nam)
试试这个:

导入itertools
对于itertools.组合中的x(df.列,2):
df[x[0]+“x”+x[1]]=df[x[0]]*df[x[1]]
编辑:不带itertools的解决方案

def组合(iterable,r):
#组合('ABCD',2)->AB AC AD BC BD CD
#组合(范围(4),3)->012 013 023 123
pool=元组(iterable)
n=长(池)
如果r>n:
返回
索引=列表(范围(r))
收益元组(索引中i的池[i]
尽管如此:
对于反向(范围(r))中的i:
如果索引[i]!=i+n-r:
打破
其他:
返回
指数[i]+=1
对于范围(i+1,r)内的j:
指数[j]=指数[j-1]+1
收益元组(索引中i的池[i]
k=3
对于组合中的x(df.列,k):
df[“X”。连接(X)]=df[列表(X)].prod(轴=1)

我还更新了代码,以便您可以将k指定为您想要的任何值。来自

的组合函数之所以发生这种情况,是因为当您迭代内部循环并更改df列时,df正在更改大小

要解决此问题,请在循环之前定义另一个大小不变的变量

import pandas as pd

df = pd.DataFrame({'a':[1,2,3], 'b':[2,3,4], 'c':[3,4,5]})

orig_size = df.columns.size

for i in range(0, orig_size):
    nam = []
    for j in range (i + 1, orig_size):
        col1 = str(df.columns[i])
        col2 = str(df.columns[j])
        nam = col1 + "X" + col2 
        df[nam] = pd.Series(df[col1] * df[col2], name=nam)

终端中的输出:

>>> df
   a  b  c  aXb  aXc  bXc
0  1  2  3    2    3    6
1  2  3  4    6    8   12
2  3  4  5   12   15   20

伟大的解决方案+1@Uni13这回答了你的问题吗?