[英]How to create a new column from an existing column in a pandas dataframe
I have the following pandas dataframe:我有以下 pandas dataframe:
df = pd.DataFrame([-0.167085, 0.009688, -0.034906, -2.393235, 1.006652],
index=['a', 'b', 'c', 'd', 'e'],
columns=['Feature Importances'])
Output: Output:
Without having to use any loops, what is the best way to create a new column (say called Difference from 0
) where the values in this new Difference from 0
column are the distances from 0 for each of the Feature Importances
values.在不必使用任何循环的情况下,创建新列的最佳方法是什么(例如称为Difference from 0
),其中这个新的Difference from 0
列中的值是每个Feature Importances
值与 0 的距离。 For eg.例如。 for a
, Difference from 0
value would be 0 - (-0.167085) = 0.167085, for e
, Difference from 0
value would be 1.006652 - 0 = 1.006652, etc.对于a
, Difference from 0
值的差值为 0 - (-0.167085) = 0.167085,对于e
, Difference from 0
值的差值为 1.006652 - 0 = 1.006652 等。
Many thanks in advance.提前谢谢了。
assign a column that operates as the absolute value on the feature importance column:在特征重要性列上分配一个作为绝对值操作的列:
df["Difference from 0"] = df["Feature Importances"].abs()
Looks like you want to find the difference from zero and also get the absolute value.看起来您想找到与零的差异并获得绝对值。
This should give you the results.这应该会给你结果。
df['Diff'] = df['Feature Importances'].abs() - 0
Feature Importances Diff
a -0.167085 0.167085
b 0.009688 0.009688
c -0.034906 0.034906
d -2.393235 2.393235
e 1.006652 1.006652
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.