Python Merge function isn't working as result appear as "Nan"

Viewed 54

I have two dataframes that I need to merge but the results kept sending (NAN) values. I changed the how but still no impact.

Used Function

PG =pd.merge(left=PG.astype(str), right=SCH.astype(str), how='left',left_on='Merged', right_on='Merged' , suffixes=('', '_remove'))

DF 1

                       Merged
0    140042022-05-09 00:00:00
1     27942022-05-10 00:00:00
2    140042022-05-11 00:00:00
3    140062022-05-10 00:00:00
4    140062022-05-11 00:00:00
..                        ...

DF2

                             Merged Today-Shift
0      147212022-03-01 00:00:00    0.291667
1      147212022-03-02 00:00:00    0.291667
2      147212022-03-03 00:00:00    0.291667
3      147212022-03-04 00:00:00    0.291667
4      147212022-03-05 00:00:00         OFF
...                         ...         ...

Result

        Today-Shift
0           NaN
1           NaN
2           NaN
3           NaN
4           NaN
1 Answers

According to Pandas documentation on merge, we can perform any of the {‘left’, ‘right’, ‘outer’, ‘inner’, ‘cross’} join on two dataframes. If we want to perform inner join on the df_2 with df_1, it will result in only the common rows among these two dataframes. Here is an example of inner join on two dataframes:

import pandas as pd

df_1 = pd.DataFrame({
    'Merged': ["140042022-05-09 00:00:00",
               "27942022-05-10 00:00:00",
               "147212022-03-01 00:00:00"]
})

df_2 = pd.DataFrame({
    'Merged': ["147212022-03-01 00:00:00",
               "147212022-03-02 00:00:00",
               "147212022-03-03 00:00:00"],
    'Today-Shift': [0.291667, 0.291667, "OFF"]
})

df_3 = df_2.merge(df_1, how='inner', left_on='Merged', right_on='Merged')
print(df_3)

Output:

                     Merged Today-Shift
0  147212022-03-01 00:00:00    0.291667

References:

Related