![](/img/trans.png)
[英]How to create new pandas dataframe from the top 10 of a certain column from existing dataframe
[英]How to create a new column from an existing column in a 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:
在不必使用任何循环的情况下,创建新列的最佳方法是什么(例如称为Difference from 0
),其中这个新的Difference from 0
列中的值是每个Feature Importances
值与 0 的距离。 例如。 对于a
, Difference from 0
值的差值为 0 - (-0.167085) = 0.167085,对于e
, Difference from 0
值的差值为 1.006652 - 0 = 1.006652 等。
提前谢谢了。
在特征重要性列上分配一个作为绝对值操作的列:
df["Difference from 0"] = df["Feature Importances"].abs()
看起来您想找到与零的差异并获得绝对值。
这应该会给你结果。
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.