Pandas:如何根据另一列的值创建一列? [英] Pandas: How to create a column based on values of another column?
本文介绍了Pandas:如何根据另一列的值创建一列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在数据框的末尾创建一个新列,其中该新列中的值是应用某些参数基于其他列的函数的结果.具体来说,来自另一列,但不同的行.例如,如果我的数据框有两列,分别包含值 x_i
, y_i
,我的第三列将是 f(x_(i-1),y_(i-1))
I need to create a new column at the end of a data frame, where the values in that new column are the result of applying some function who's parameters are based on other columns. Specifically, from another column, but a different row. So for example, if my data frame had two columns, containing values x_i
, y_i
respectively, my third column would be f(x_(i-1), y_(i-1))
我知道要创建一个新列,最简单的方法是执行类似
I know that to create create a new column, the easiest way would be to do something like
df['new_row'] = ...
但我不确定我可以设置什么.
But I'm not sure what I can set to that.
我该怎么做?
推荐答案
1 3
3 3
43 4
2 3
with open("file", 'r') as f:
for line in f:
n, r = line.split()
formula = pow(int(n),int(r))
print("{:4}{:4}{:9}".format(n,r, formula))
输出
1 3 1
3 3 27
43 4 3418801
2 3 8
这篇关于Pandas:如何根据另一列的值创建一列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文