![](/img/trans.png)
[英]Offsetting an existing date value, where values exist in another column in dataframe
[英]Check if each column values exist in another dataframe column where another column value is the column header
companies.xlsx
company To
1 amazon hi@test.de
2 google bye@test.com
3 amazon hi@tld.com
4 starbucks hi@test.de
5 greyhound bye@tuz.de
emails.xlsx
hi@test.de bye@test.com hi@tld.com ...
1 amazon google microsoft
2 starbucks amazon tesla
3 Grey Hound greyhound
4 ferrari
所以我有上面的 2 張 excel 表並閱讀了兩個 em:
file1 = pd.ExcelFile('data/companies.xlsx')
file2 = pd.ExcelFile('data/emails.xlsx')
df_companies = file1.parse('sheet1')
df_emails = file2.parse('sheet1')
我想要完成的是:
例如:亞馬遜公司在 company.xlsx 中有 To email hi@test.de。 在 email.xlsx 中存在 header hi@test.de 並且在列中也找到了亞馬遜 - 所以它是“1”。
任何人都知道如何做到這一點?
這是一種方法。 將df_emails
轉換為字典,並將 map 轉換為df_companies
。 然后,將映射列與df_companies['company']
進行比較。
df_companies['check'] = df_companies['To'].map(df_emails.to_dict(orient='list')).fillna('')
df_companies['check'] = df_companies.apply(lambda x: x['company'] in x['check'], axis=1).astype(int)
company To check
1 amazon hi@test.de 1
2 google bye@test.com 1
3 amazon hi@tld.com 0
4 starbucks hi@test.de 1
5 greyhound bye@tuz.de 0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.