Python 2.7 Python&x27;以另一列中的观察值为条件,在一列中添加

Python 2.7 Python&x27;以另一列中的观察值为条件,在一列中添加,python-2.7,pandas,Python 2.7,Pandas,我在python中有以下数据帧: current_data = pd.DataFrame({'X': ['3'+'*NY', '3', '2', '2'+'*NY', '1', '7'], 'Y': [np.nan, 4, 5, np.nan, 8, np.nan]}) 我想得到的是: needed_data = pd.DataFrame({'X': ['3'+'*NY', '3', '2', '2'+'*NY', '1', '7'], 'Y': [4, 4, 5, 5, 8, np.nan]

我在python中有以下数据帧:

current_data = pd.DataFrame({'X': ['3'+'*NY', '3', '2', '2'+'*NY', '1', '7'], 'Y': [np.nan, 4, 5, np.nan, 8, np.nan]})
我想得到的是:

needed_data = pd.DataFrame({'X': ['3'+'*NY', '3', '2', '2'+'*NY', '1', '7'], 'Y': [4, 4, 5, 5, 8, np.nan]})

因此,我想用“*NY”部分替换Y列中与X中的观测值相对应的nan,将Y中与X中的观测值相对应的数字替换为具有相同数字部分但没有“*NY”

这对代码来说有点烦人,基本上我们可以应用自定义函数为您执行查找:

In [106]:
# define our function
def func(x):
    # test to see if the asterisk is present
    if x.find('*') > 0:
        # perform a lookup on a slice of the passed in string
        return(current_data.loc[current_data.X==x[0:x.find('*')],'Y'].values.max())
# using loc assign to column 'Y' where it is null the returned calculation of the apply
current_data.loc[current_data.Y.isnull(),'Y'] = current_data[current_data.Y.isnull()]['X'].apply(func)
current_data
Out[106]:
      X   Y
0  3*NY   4
1     3   4
2     2   5
3  2*NY   5
4     1   8
5     7 NaN

您的代码没有运行,而且有点不清楚,您可以发布所需的输出吗?代码现在必须正常工作。我不知道如何在这里发布python输出,所以我以代码的形式发布,因为您的代码不起作用,我无法可视化所需的输出,现在我可以了。然而,您的解释令人困惑,因为Y值是
NaN
您想查找相应的
Y
值,其中
X
值与
3xNY
的数字分量匹配,对吗?是的,这就是它的工作原理:)谢谢,我想我需要花一个小时左右的时间来了解它的工作原理,但无论如何,谢谢