0
$\begingroup$

I've two Dataframes

  1. DF1 = Contains (~30 Columns and 15000 entries) relevant are "TYP" (string) and "VERSION" (string)
  2. DF2 = Contains "SOFTWARE_NAME" (string), "VERSION" (string) and "EOS" (date)

Now I'd like to add in DF1 additional columns "EOS_DATE" (date), "EOS" (bool) if

  DF1['TYP'] == DF2['SOFTWARE_NAME'] &
  DF1['VERSION'] == DF2['VERSION']

then

  DF1['EOS_DATE'] = DF2['EOS']
  DF1['EOS'] = if DF2['EOS'] < now() then True else False

I've tried with np, pd.where(~) but running mainly in this issue:

ValueError: Can only compare identically-labeled Series objects
$\endgroup$
3
  • $\begingroup$ To perform this comparison it's important to know how the two dataframe relate to each other (if at all). Is there a key that can be used to connect the two or is there some other way to relate the two dataframes? $\endgroup$
    – Oxbowerce
    Commented Nov 3, 2021 at 12:48
  • 1
    $\begingroup$ This is a code question. You can totally post in on Stack Overflow, with "Pandas" tag, you might get a bit more answers $\endgroup$
    – Adept
    Commented Nov 3, 2021 at 14:55
  • $\begingroup$ @Oxbowerce ... they are build separately from different sources. The only common what they have is the TYP <-> SOFTWARE_NAME and VERSION <-> VERSION. No other keys available. $\endgroup$
    – Peter
    Commented Nov 3, 2021 at 15:28

1 Answer 1

1
$\begingroup$

First, change your columns values, so you won't have any issue (my goal is to make only one dataframe)

DF2 = DF2.rename(columns={"EOS": "DF2_EOS", "VERSION": "DF2_VERSION"})

Then you want to apply a change only for lines valuing your conditions. The best to do is to merge your dataframes, keeping all your DF1 rows (ie : standard merge) :

DF1 = DF1.merge(DF2, left_on=['TYP', 'VERSION'], right_on=['SOFTWARE_NAME', 'DF2_VERSION'])

Finally make your changes :

DF1.loc[DF1['DF2_EOS'] < pandas.to_datetime('today').normalize(), 'EOS'] = True
DF1.loc[DF1['DF2_EOS'] >= pandas.to_datetime('today').normalize(), 'EOS'] = False
DF1 = DF1.rename(columns={"DF2_EOS": "EOF_DATE"})
$\endgroup$
3
  • 1
    $\begingroup$ Instead of two loc assignments for True and False, you should be able to assign a single boolean Series: DF1['EOS'] = DF1['DF2_EOS'] < pd.to_datetime('now').normalize() $\endgroup$
    – tdy
    Commented Nov 3, 2021 at 16:28
  • $\begingroup$ Hard to confirm without an MRE, though. $\endgroup$
    – tdy
    Commented Nov 3, 2021 at 16:29
  • $\begingroup$ @tdy -- Understand, promise to describe better the next time. $\endgroup$
    – Peter
    Commented Nov 4, 2021 at 7:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.