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

Python 有效地减去数据帧中的每一列?

Python 有效地减去数据帧中的每一列?,python,pandas,data-science,Python,Pandas,Data Science,例如,我有一个如下所示的数据框,我想创建一个新的数据框,其中包含列作为以前列的减法 给定的数据帧:(可以有任意数量的列) 多谢各位 编辑:使用itertools.permutations代替Austin Wagner评论中指出的itertools.combines,以确定一个命令中所有可能的组合 您需要确定所有可能的组合,并计算每个组合的减法结果 df = pd.DataFrame({ 'A': [10, 15, 20, 25, 30], 'B': [100, 115, 120,

例如,我有一个如下所示的数据框,我想创建一个新的数据框,其中包含列作为以前列的减法

给定的数据帧:(可以有任意数量的列)

多谢各位

编辑:使用itertools.permutations代替Austin Wagner评论中指出的itertools.combines,以确定一个命令中所有可能的组合

您需要确定所有可能的组合,并计算每个组合的减法结果

df = pd.DataFrame({
    'A': [10, 15, 20, 25, 30],
    'B': [100, 115, 120, 125, 130],
    'C': [200, 215, 220, 225, 230]
})

def subtractions_of_combinations(df):
    # Extract all the combinations
    combinations = list(itertools.permutations(df.columns, 2)) 
    # Calculate the two possible subtractions for each combination
    new_df = pd.DataFrame()
    for a, b in combinations:
        new_df[f'{a}-{b}'] = df[a] - df[b]
    return new_df

如果您想要所有可能的组合,您可能需要使用itertools置换。无需在get col just
itertools.permutations(df.columns,2)中进行列表理解:
非常感谢。
df = pd.DataFrame({
    'A': [10, 15, 20, 25, 30],
    'B': [100, 115, 120, 125, 130],
    'C': [200, 215, 220, 225, 230]
})

def subtractions_of_combinations(df):
    # Extract all the combinations
    combinations = list(itertools.permutations(df.columns, 2)) 
    # Calculate the two possible subtractions for each combination
    new_df = pd.DataFrame()
    for a, b in combinations:
        new_df[f'{a}-{b}'] = df[a] - df[b]
    return new_df