![](/img/trans.png)
[英]Pandas: Check if value in one df exists in any column of another DF
[英]Length error when trying to check if value exists in another pandas column
我想返回一個反映P2 Actual Scan Site
的Scoring Site
列,但前提是P2 Actual Scan Site
被認為是有效的。
輸入:
dataframe sites_exams_df
包括檢查有效性的列Valid Site for Exam?
, 像這樣:
SCI DBN4 Valid Site for Exam?
0 NaN 02L298 NaN
1 NaN 02L416 NaN
2 NaN 02L420 NaN
3 SCI, 02L475 02L475
我當前的 dataframe 與P2 Actual Scan Site
:
P2 Actual Scan Site
0 -
1 10L445
2 10L445
3 02L475
4 02L475
5 27L400
6 00000C
所需的 Output:
P2 Actual Scan Site Scoring Site
0 - NaN
1 10L445 10L445
2 10L445 10L445
3 02L475 02L475
4 02L475 02L475
5 27L400 27L400
6 00000C NaN
請注意,最后一個學校代碼返回 NaN,因為它不被視為有效站點(以及-
)。
所有其他學校代碼按原樣返回。
當前代碼:
# 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 = sites_exams_df['Valid Site for Exam?'].dropna().values
# attempt 1
df['Scoring Site'] = np.where(df['P2 Actual Scan Site'] in valid_sites,
df['P2 Actual Scan Site'],np.nan)
# attempt 2
df['Scoring Site'] = df.apply(lambda x: df['P2 Actual Scan Site'] if x in valid_sites else np.nan)
兩者都給我長度錯誤:
嘗試 1 - raise ValueError("Lengths must match to compare") ValueError: Lengths must match to compare
嘗試 2 - ValueError: ('Lengths must match to compare', 'occurred at index DBN - Exam')
這應該可以解決您的問題:
# 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 是應該使用的操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.