在Python 3和Pandas中计算一列中的两个不同公式

在Python 3和Pandas中计算一列中的两个不同公式,python,python-3.x,pandas,Python,Python 3.x,Pandas,我目前有以下数据帧,并希望用漂移值(解释如下)填充空的“权重”单元格 对于“重量”列中高于234的值,我希望有以下值: 对于低于234的值,我希望有以下值: 要实现我在Python 3和Pandas中的目标,最好和最具Python风格的方法是什么?(我愿意使用内置了这种转换的库;但是,我自己还没有找到。) 编辑1:上面的文本版本: 以下是上述数据帧的简单版本 A B 0 1 NaN 1 1 3 2 4 NaN 我希望B列中高于值“3”的所有值采用以下公式。注:撇号内的

我目前有以下数据帧,并希望用漂移值(解释如下)填充空的“权重”单元格

对于“重量”列中高于234的值,我希望有以下值:

对于低于234的值,我希望有以下值:

要实现我在Python 3和Pandas中的目标,最好和最具Python风格的方法是什么?(我愿意使用内置了这种转换的库;但是,我自己还没有找到。)

编辑1:上面的文本版本:

以下是上述数据帧的简单版本

   A  B
0  1  NaN
1  1  3
2  4  NaN
我希望B列中高于值“3”的所有值采用以下公式。注:撇号内的值是表示上述数据框的x和y标签的坐标

   A  B
0  1  NaN
1  1  3
2  4  NaN
“1B”/(1+“0B”)

我希望B列中值“3”以下的所有值采用以下公式

“1B”*(1+“2A”)


请注意,坐标与当前单元格的相对距离不会改变。在Excel术语中,引用是“相对的”,而不是“绝对的”

由于值是从先前计算的值派生的,因此必须使用类似于for循环的方法来实现这一点:

# Grabbing index of non-empty row
j = int(df[df["weight"].notnull()].index[0])

# Logic for forward-filling values
for i in range(j+1, df.shape[0]):
    df.loc[i,'weight'] = df.loc[i-1,'weight'] * (1 + df.loc[i,'return'])

# Logic for backward-filling values
for i in range(j-1, -1, -1):
    df.loc[i,'weight'] = df.loc[i+1,'weight'] / (1 + df.loc[i,'return'])

使用此示例数据:

   return  weight
0       1     NaN
1       1     3.0
2       4     NaN
我们有这样的结果:

   return  weight
0       1     1.5
1       1     3.0
2       4    15.0

由于值是从先前计算的值派生的,因此必须使用类似于for循环的方法来实现这一点:

# Grabbing index of non-empty row
j = int(df[df["weight"].notnull()].index[0])

# Logic for forward-filling values
for i in range(j+1, df.shape[0]):
    df.loc[i,'weight'] = df.loc[i-1,'weight'] * (1 + df.loc[i,'return'])

# Logic for backward-filling values
for i in range(j-1, -1, -1):
    df.loc[i,'weight'] = df.loc[i+1,'weight'] / (1 + df.loc[i,'return'])

使用此示例数据:

   return  weight
0       1     NaN
1       1     3.0
2       4     NaN
我们有这样的结果:

   return  weight
0       1     1.5
1       1     3.0
2       4    15.0

因为您知道您使用的是权重列中只有一个值的已知数据,所以请查找该项的索引和值

然后,只需在该指数上方使用一个计算,在下方使用另一个计算。 请注意,索引切片在这里是包含的:

import pandas as pd
import numpy as np

df = pd.DataFrame(data={'A': [i for i in range(10)], 'B': np.nan})
df.at[4,'B'] = 10

given_idx = df[df.B.notnull()].index.values[0]
given_val = df.at[given_idx, 'B']

df.loc[:given_idx-1, 'B'] = df['A'] + given_val
df.loc[given_idx+1:, 'B'] = df['A'] - given_val

print(df)

因为您知道您使用的是权重列中只有一个值的已知数据,所以请查找该项的索引和值

然后,只需在该指数上方使用一个计算,在下方使用另一个计算。 请注意,索引切片在这里是包含的:

import pandas as pd
import numpy as np

df = pd.DataFrame(data={'A': [i for i in range(10)], 'B': np.nan})
df.at[4,'B'] = 10

given_idx = df[df.B.notnull()].index.values[0]
given_val = df.at[given_idx, 'B']

df.loc[:given_idx-1, 'B'] = df['A'] + given_val
df.loc[given_idx+1:, 'B'] = df['A'] - given_val

print(df)
请提供一份清单。这意味着没有图像/链接,只有文本。另请参见。请提供一份。这意味着没有图像/链接,只有文本。另见。