简体   繁体   English

如何从 pandas dataframe 中的现有列创建新列

[英]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:

df - 创建新列

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.对于aDifference from 0值的差值为 0 - (-0.167085) = 0.167085,对于eDifference 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.

相关问题 如何从现有数据框的某列的前10个创建新的熊猫数据框 - How to create new pandas dataframe from the top 10 of a certain column from existing dataframe 如何将新列分配给 Pandas 中的现有 DataFrame - How to assign new column to existing DataFrame in pandas 如何将新列添加到现有 pandas dataframe - How to add a new column to an existing pandas dataframe 如何在现有 pandas dataframe 中填充新列 - How to populate a new column in an existing pandas dataframe 如何从数据框中的其他列创建新的Pandas数据框列 - How to create a new Pandas dataframe column from other columns in the dataframe 如何使用 Pandas 使用来自第二个数据帧但依赖于当前数据帧中不同现有列的值填充新列 - How To Fill New Column With Values From Second Dataframe but Dependent on Different Existing Column in Current Dataframe using Pandas 如何在 Pandas 中创建一个新列并根据现有列中的条件值向该新列添加值? - How to create a new column in pandas and add value to that new column based on the conditional value from the existing column? 如何使用现有列名称和值在Pandas数据框中创建列表的新列? - How to create new column of lists in Pandas dataframe using existing column names and values? 从列表列创建一个新的 Pandas dataframe - Create a new Pandas dataframe from column of lists 如何从包含 json 的文件创建新的 pandas dataframe 列? - How to create new pandas dataframe column from file that contains json?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM