Python 数据框 - 如何 evaluate/use 动态创建列
Python Data Frame - How can I evaluate/use a column being created on the fly
假设我有一个数据框如下:
+---------+-------+------------+
| Product | Price | Calculated |
+---------+-------+------------+
| A | 10 | 10 |
| B | 20 | NaN |
| C | 25 | NaN |
| D | 30 | NaN |
+---------+-------+------------+
以上可以使用下面的代码创建:
data = {'Product':['A', 'B', 'C', 'D'],
'Price':[10, 20, 25, 30],
'Calculated':[10, np.nan, np.nan, np.nan]}
df = pd.DataFrame(data)
我想即时更新列 calculated
。对于第二行,第 2 行的 calculated = Prv. calculated / Previous Price
即 calculated
是 10/10=1
现在我们有了第 2 行 calculated
的值,第 3 行 calculated
将是 1/20
等等。
预期输出
+---------+-------+------------+
| Product | Price | Calculated |
+---------+-------+------------+
| A | 10 | 10 |
| B | 20 | 1 |
| C | 25 | 0.05 |
| D | 30 | 0.002 |
+---------+-------+------------+
以上可以使用循环来实现,但我不想使用循环,而是需要一种矢量化方法来更新列 Calculated
。我怎样才能做到这一点?
您正在查看 cumprod
shift
:
# also `df['Calculated'].iloc[0]` instead of `.ffill()`
df['Calculated'] = df['Calculated'].ffill()/df.Price.cumprod().shift(fill_value=1)
输出:
Product Price Calculated
0 A 10 10.000
1 B 20 1.000
2 C 25 0.050
3 D 30 0.002
假设我有一个数据框如下:
+---------+-------+------------+
| Product | Price | Calculated |
+---------+-------+------------+
| A | 10 | 10 |
| B | 20 | NaN |
| C | 25 | NaN |
| D | 30 | NaN |
+---------+-------+------------+
以上可以使用下面的代码创建:
data = {'Product':['A', 'B', 'C', 'D'],
'Price':[10, 20, 25, 30],
'Calculated':[10, np.nan, np.nan, np.nan]}
df = pd.DataFrame(data)
我想即时更新列 calculated
。对于第二行,第 2 行的 calculated = Prv. calculated / Previous Price
即 calculated
是 10/10=1
现在我们有了第 2 行 calculated
的值,第 3 行 calculated
将是 1/20
等等。
预期输出
+---------+-------+------------+
| Product | Price | Calculated |
+---------+-------+------------+
| A | 10 | 10 |
| B | 20 | 1 |
| C | 25 | 0.05 |
| D | 30 | 0.002 |
+---------+-------+------------+
以上可以使用循环来实现,但我不想使用循环,而是需要一种矢量化方法来更新列 Calculated
。我怎样才能做到这一点?
您正在查看 cumprod
shift
:
# also `df['Calculated'].iloc[0]` instead of `.ffill()`
df['Calculated'] = df['Calculated'].ffill()/df.Price.cumprod().shift(fill_value=1)
输出:
Product Price Calculated
0 A 10 10.000
1 B 20 1.000
2 C 25 0.050
3 D 30 0.002