繁体   English   中英

如何从 pandas 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:

df - 创建新列

在不必使用任何循环的情况下,创建新列的最佳方法是什么(例如称为Difference from 0 ),其中这个新的Difference from 0列中的值是每个Feature Importances值与 0 的距离。 例如。 对于aDifference from 0值的差值为 0 - (-0.167085) = 0.167085,对于eDifference 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM