简体   繁体   English

如何根据 pandas 中的现有列创建新列?

[英]How do I create a new column based on existing columns in pandas?

I am new to Python/pandas.我是 Python/熊猫的新手。 I want to compute continuous returns based on "GOOG" Price.我想根据“GOOG”价格计算连续回报。 If the price is in column (a);如果价格在 (a) 栏中; How should I calculate the return in column (b) according to the following formula?我应该如何根据以下公式计算(b)栏中的回报?

continuous returns =持续回报 =

在此处输入图像描述

I want to do this like the image below (calculating continuous returns in Excel) in Pandas DataFrame.我想在 Pandas DataFrame 中像下图那样执行此操作(在 Excel 中计算连续回报)。

在此处输入图像描述

import pandas as pd

x = pd.DataFrame([2340, 2304, 2238, 2260, 2315, 2318, 2300, 2310, 2353, 2350],
                 columns=['a'])

Try:尝试:

x['b'] = np.log(x['a']/x['a'].shift())

Output: Output:

      a         b
0  2340       NaN
1  2304 -0.015504
2  2238 -0.029064
3  2260  0.009782
4  2315  0.024045
5  2318  0.001295
6  2300 -0.007796
7  2310  0.004338
8  2353  0.018444
9  2350 -0.001276

You can use generator function with .apply :您可以将生成器 function 与.apply一起使用:

import numpy as np
import pandas as pd

x = pd.DataFrame(
    [2340, 2304, 2238, 2260, 2315, 2318, 2300, 2310, 2353, 2350], columns=["a"]
)


def fn():
    old_a = np.nan
    a = yield
    while True:
        new_a = yield np.log(a / old_a)
        a, old_a = new_a, a


s = fn()
next(s)
x["b"] = x["a"].apply(lambda v: s.send(v))
print(x)

Prints:印刷:

      a         b
0  2340       NaN
1  2304 -0.015504
2  2238 -0.029064
3  2260  0.009782
4  2315  0.024045
5  2318  0.001295
6  2300 -0.007796
7  2310  0.004338
8  2353  0.018444
9  2350 -0.001276

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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