简体   繁体   English

根据其他列名创建自定义列名 pandas dataframe

[英]Creating custom names for columns based on other column names in pandas dataframe

I have a dataframe like below:我有一个如下所示的 dataframe:

在此处输入图像描述

I am looking to create a column using difference or use any other calculations among columns.我正在寻找使用差异创建列或在列之间使用任何其他计算。 However, I looking to name the column so that it relfects the operation done.但是,我希望为该列命名,以反映已完成的操作。 For ex below I am finding the difference b/w Origin 1 and Dest 1 as below:对于下面的例子,我发现黑白 Origin 1 和 Dest 1 的区别如下:

在此处输入图像描述

How do I create those custom naming of columns as highlighted and especially when I have to create multiple such columns.如何创建突出显示的那些自定义命名的列,尤其是当我必须创建多个这样的列时。

Just iterate through it and for naming you can use a f-string只需遍历它并命名你可以使用 f-string

for col_a in df.columns:
   for col_b in df.columns:
      if col_a != col_b:
         df[f'{col_a} - {col_b}'] = df[col_a] - df[col_b]

if you use itertools (pre-installed in python) you can make it easier to read (as proposed by @MustafaAydın):如果您使用 itertools(预安装在 python 中),您可以使其更易于阅读(如@MustafaAydın 所建议):

import itertools
for col_a, col_b in itertools.permutations(df, 2):
    df[f'{col_a} - {col_b}'] = df[col_a] - df[col_b]

if you want to do multiple operations just add a line如果你想做多个操作只需添加一行

import itertools
for col_a, col_b in itertools.permutations(df, 2):
    df[f'{col_a} - {col_b}'] = df[col_a] - df[col_b]
    df[f'{col_a} + {col_b}'] = df[col_a] + df[col_b]

if you only want to use subsets of columns, eg only from origin to destination you can do:如果您只想使用列的子集,例如仅从起点到终点,您可以这样做:

import itertools
origins = [col for col in df.columns if col.startswith('Origin')]
destinations = [col for col in df.columns if col.startswith('Dest')]
for col_a, col_b in itertools.product(origins, destinations):
    df[f'{col_a} - {col_b}'] = df[col_a] - df[col_b]
    df[f'{col_a} + {col_b}'] = df[col_a] + df[col_b]

It is quite simple.这很简单。

Let's define a dataframe with two columns a and b :让我们定义一个 dataframe 有两列ab

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

Output: Output:

    a   b
0   1   4
1   2   3
2   3   2
3   4   1

Now, let's create the difference mentioned above between the two columns现在,让我们在两列之间创建上面提到的差异

df["a-b"] = df["a"] - df["b"]

Voila.瞧。 Now you have a new column.现在您有了一个新列。

    a   b   a-b
0   1   4   -3
1   2   3   -1
2   3   2   1
3   4   1   3

For multiple iterative calculation, we can workout loop-based approach:对于多次迭代计算,我们可以采用基于循环的方法:

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

df["a-b"] = df["a"] -df["b"]

#if you want to calculate for every column combination
for i in df.columns:
    for j in df.columns:
        if i != j and "-" not in j and "-" not in i:
            df[f"{i}-{j}"] = df[i] - df[j]

This approach calculates all differences between all columns in one loop.这种方法计算一个循环中所有列之间的所有差异。

Output: Output:

    a   b   c   a-b a-c b-a b-c c-a c-b
0   1   4   8   -3  -7  3   -4  7   4
1   2   3   7   -1  -5  1   -4  5   4
2   3   2   6   1   -3  -1  -4  3   4
3   4   1   5   3   -1  -3  -4  1   4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pandas:根据现有dataframe中列的名称和数据创建新的dataframe - Pandas: creating new dataframe based on the names and data of columns in existing dataframe 通过合并其他列并根据先前的列名重命名,将新列添加到pandas数据框中 - Adding a new column to the pandas dataframe by merging other columns and renaming based on previous column names 根据与其他列名称匹配的列值填充 Pandas Dataframe - Populate Pandas Dataframe Based on Column Values Matching Other Column Names 根据行值使用其他列的名称填充新的 Pandas 数据框列 - Populate a new pandas dataframe column with names of other columns based on their row value Pandas DataFrame使用其他列的名称聚合列作为值 - Pandas DataFrame aggregated column with names of other columns as value Pandas Dataframe 基于其他列创建新列 - Pandas Dataframe Creating a New Column Based on Other Columns Pandas dataframe:根据其他列的数据创建新列 - Pandas dataframe: Creating a new column based on data from other columns Pandas数据框:使用另外2列创建一个新列,该列是自定义函数 - Pandas dataframe: creating a new column that is a custom function using 2 other columns 根据列名称转换熊猫中的数据框 - Convert a dataframe in pandas based on column names 基于列名称的Pandas数据框条件均值 - Pandas dataframe conditional mean based on column names
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM