[英]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 有两列a
和b
:
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.