Python 如何将单元格值传递给shift函数

Python 如何将单元格值传递给shift函数,python,pandas,variables,shift,Python,Pandas,Variables,Shift,如何将数据帧中的单元格值传递给shift函数 以下是一些示例输入: import pandas as pd import numpy as np df = pd.DataFrame(data={'x': [0,0,0,0,0,0,0,5,0,0], 'y': [np.nan,np.nan,np.nan,10,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan]}) df['z'] = np.where(df['x

如何将数据帧中的单元格值传递给shift函数

以下是一些示例输入:

import pandas as pd
import numpy as np

df = pd.DataFrame(data={'x': [0,0,0,0,0,0,0,5,0,0],
                        'y': [np.nan,np.nan,np.nan,10,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan]})

df['z'] = np.where(df['x'].shift(1) > 0, (50 - df['y'].shift(5)), np.nan)

print(df)

df['a'] = np.where(df['x'].shift(1) > 0, (50 - df['y'].shift(df['x'].shift(1).get_value())), np.nan)
以下是输出:

   x     y     z
0  0   NaN   NaN
1  0   NaN   NaN
2  0   NaN   NaN
3  0  10.0   NaN
4  0   NaN   NaN
5  0   NaN   NaN
6  0   NaN   NaN
7  5   NaN   NaN
8  0   NaN  40.0
9  0   NaN   NaN

Traceback (most recent call last):
  File "C:\stocks\sandp500\stack overflow question 1.py", line 11, in <module>
    df['a'] = np.where(df['x'].shift(1) > 0, (50 - df['y'].shift(df['x'].shift(1).get_value())), np.nan)
TypeError: get_value() missing 1 required positional argument: 'label'
xyz
0 0楠楠楠
10楠楠楠
20南南
3010.0南
40南南
50南南
南南60号
7.5楠楠
80.40.0
9 0楠楠
回溯(最近一次呼叫最后一次):
文件“C:\stocks\sandp500\stack overflow question 1.py”,第11行,在
df['a']=np.where(df['x'].shift(1)>0,(50-df['y'].shift(df['x'].shift(1.get_value()),np.nan)
TypeError:get_value()缺少1个必需的位置参数:“label”
列“x”的值为0或是1到n之间的某个整数。这些整数是我想要传递给shift函数以创建列“z”的值。在“z”列中,我通过将“5”硬编码到移位函数中来作弊。列“a”是我尝试将动态值从列“x”传递给shift函数的过程

在过去的48小时里,我尝试了几十种不同的方法,但都没有成功。
有人有想法吗?提前感谢。

我认为您试图通过
df实现的目标是可以实现的。应用

df = pd.DataFrame(data={'x': [0,0,0,0,0,0,0,5,0,0],
                        'y': [np.nan,np.nan,np.nan,10,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan]})
我认为在独立函数中遵循逻辑比在lambda中更容易:

def shifter(mydf):
    offset = int(mydf['offset'])
    if offset>0:
        val = 50 - df.shift(int(offset))['y'].loc[int(mydf['index'])]
    else:
        val = np.nan
    return val    
我制作了一个数据帧的副本,索引作为一列,然后因为您在“x”列上做了额外的
.shift(1)
,我制作了一个名为
offset
的辅助列,它只是提前做了这件事:

df2 = df.reset_index()
df2['offset'] = df2['x'].shift(1).fillna(0)

print(df2)
   index  x     y  offset
0      0  0   NaN     0.0
1      1  0   NaN     0.0
2      2  0   NaN     0.0
3      3  0  10.0     0.0
4      4  0   NaN     0.0
5      5  0   NaN     0.0
6      6  0   NaN     0.0
7      7  5   NaN     0.0
8      8  0   NaN     5.0
9      9  0   NaN     0.0
然后,您可以在行的基础上应用移位器功能。注意,它仍然引用该函数中的原始
df
数据帧。它使用“索引”列中的值通过原始数据帧上的
.loc
查找调整后的行

df['z'] = df2.apply(shifter, axis=1)

print(df)
   x     y     z
0  0   NaN   NaN
1  0   NaN   NaN
2  0   NaN   NaN
3  0  10.0   NaN
4  0   NaN   NaN
5  0   NaN   NaN
6  0   NaN   NaN
7  5   NaN   NaN
8  0   NaN  40.0
9  0   NaN   NaN
相关,仅供参考,如果您只是想获取偏移值,而不需要额外的逻辑:

df = pd.DataFrame(data={'x': [0,1,3,5,2,1,0,5,0,1],
                        'y': [2.0,4,3,10,1,77,28,56,42,48]})

def simple_shifter(mydf):
    offset = int(mydf['x'])
    val = df.shift(int(offset))['y'].loc[int(mydf['index'])]
    return val    

df3 = df.reset_index()
df['y_offset_by_x'] = df3.apply(simple_shifter, axis=1)

print(df)
   x     y  y_offset_by_x
0  0   2.0            2.0
1  1   4.0            2.0
2  3   3.0            NaN
3  5  10.0            NaN
4  2   1.0            3.0
5  1  77.0            1.0
6  0  28.0           28.0
7  5  56.0            3.0
8  0  42.0           42.0
9  1  48.0           42.0

当然,如果数据帧较大,则需要进行速度优化,但这应该是可行的。

请发布
a列的预期输出。如果您也能解释其计算背后的逻辑,那就太好了。列“a”的预期输出是列“z”中当前显示的内容=除第8行40.0之外的所有NaN。其背后的逻辑是,只要“x”列中存在非零值,“z”列中的下一行就是“y”列值的简单减法(被“x”列变量整数的值移位)。此计算背后是否有逻辑?