Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 尝试检查另一列中是否存在值时出现长度错误_Python_Pandas - Fatal编程技术网

Python 尝试检查另一列中是否存在值时出现长度错误

Python 尝试检查另一列中是否存在值时出现长度错误,python,pandas,Python,Pandas,我想返回一列评分站点,该列反映P2实际扫描站点,但仅当P2实际扫描站点被视为有效时 投入: 数据框sites\u tests\u df包括一列,用于检查有效性检查的有效站点?,如下所示: SCI DBN4考试有效站点? 0 NaN 02L298 NaN 1 NaN 02L416 NaN 2南02L420南 3 SCI,02L475 02L475 我的当前数据帧与P2实际扫描站点: P2实际扫描站点 0 - 110L445 2 10L445 302L475 402L475 527

我想返回一列
评分站点
,该列反映
P2实际扫描站点
,但仅当
P2实际扫描站点
被视为有效时

投入:

数据框
sites\u tests\u df
包括一列,用于检查有效性
检查的有效站点?
,如下所示:

SCI DBN4考试有效站点?
0 NaN 02L298 NaN
1 NaN 02L416 NaN
2南02L420南
3 SCI,02L475 02L475
我的当前数据帧与
P2实际扫描站点

P2实际扫描站点
0         -
110L445
2 10L445
302L475
402L475
527L400
60万摄氏度
期望输出:

P2实际扫描站点评分站点
0-NaN
1 10L445 10L445
2 10L445 10L445
302L475 02L475
4 02L475 02L475
5 27L400 27L400
60万立方厘米南
请注意,最后一个学校代码返回NaN,因为它不被视为有效站点(以及
-

所有其他学校代码返回原样

当前代码:

#创建评分网站栏
sites_-tests_-df['Valid Site for Exam?']=np.where(sites_-tests_-df[Exam].notnull(),sites_-tests_-df['DBN4'],
np.nan)#检查本次考试有哪些评分点
valid_sites=sites_tests_df['valid Site for examing?'].dropna().values
#尝试1
df['Scoring Site']=np.其中(df['P2实际扫描站点]]在有效的_站点中,
df['P2实际扫描站点'],np.nan)
#尝试2
df['Scoring Site']=df.apply(lambda x:df['P2实际扫描站点'],如果x在有效的\u站点中,则为np.nan)
两者都会给我长度错误:

尝试1—
raisevalueerror(“长度必须匹配才能比较”)
ValueError:长度必须匹配才能进行比较

尝试2-
ValueError:(“长度必须匹配以进行比较”,“发生在索引DBN处-检查”)

这应该可以解决您的问题:

# create Scoring Site column
sites_exams_df['Valid Site for Exam?'] = np.where(sites_exams_df[exam].notnull(),sites_exams_df['DBN4'],
                                                  np.nan) # check what scoring sites are there for this exam
valid_sites = list(sites_exams_df['Valid Site for Exam?'].dropna())

df['Scoring Site'] = df['P2 Actual Scan Site'].map(lambda x: x if x in valid_sites else np.nan)
map是应该使用的操作