简体   繁体   中英

How can I compare two different datasets and return values based in a column based on a specific criteria?

I am trying to create a new column in the "schedule" dataframe after comparing two separate columns in the "other" dataframe. Here is how my code looks now:

import pandas as pd
import numpy as np
schedule = pd.read_excel('schedule.xlsx')
other = pd.read_excel('other.xlsx')

other['New Column'] = np.where(other['Termination Date'] >= schedule['Beginning'] & (other['Termination Date'] <= schedule['End'], schedule['Pay Date']))

but it is returning this error:

ValueError: Can only compare identically-labeled Series objects

Here is what a typical example would look like in this scenario:

if

other['Termination Date']= "5/22/2021"

then it would return "6/11/2021" because it would look at

schedule['Beginning']

and

schedule['End']

to meet the criteria.

Note that the two data frames do not have any similar data to merge on. Basically, I just need to compare from one data frame and return values on another. Let me know if you have any questions and thank you all in advance!

schedule df

other df

It looks like Python will only compare the values if it looking at columns with the same labels from the two tables. You are asking Python to compare the same column twice.

Perhaps try an if else type statement in place of the &...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM