Question: Need help with Pandas Data frame and a running python code without error Output Table Problem: Given two data frames, df1 and df2. Compare the

Need help with Pandas Data frame and a running python code without error

Need help with Pandas Data frame and a running python code without

error Output Table Problem: Given two data frames, df1 and df2. Compare

Output Table

the values in the LOC column in both data frames. If there

Problem: Given two data frames, df1 and df2. Compare the values in the LOC column in both data frames. If there is a difference in the same index number in both data frames while comparing, there will be a new table called DF3. The new table will merge the columns from both data frames and create two additional columns, TY and RC. TY column has three values 'Base', 'Comp', and ' Dif '. 'Base' refers to the DF1 data frame, 'Comp' refers to the DF2 data frame, and 'Dif ' refers to the difference between data frames(DF1 & DF2). Rec column refers to the index number/observation for LOC column values in which the column value doesnt match.

For example, DF1 and DF2 data frame has column LOC .If in the second record,DF1 has INY value and DF2 have sju value, in the new table column LOC would get 3 values like INY, sju and xxx. So Iny, sju & xxx will connect with respectively Base, comp & Dif under the TY column.

If there is no data in Loc column in DF1/DF2, then it will be empty in the DF3 table column(LOC). We dont have to add Dif in the TY column and xxx in the Loc column in the new table.

DF1 \begin{tabular}{|l|l|l|l|l|} \hline & P & C & S & Loc \\ \hline 1 & 120 & IN & arw & law \\ \hline 2 & 121 & US & arw & Iny \\ \hline 3 & 122 & CA & arw & \\ \hline 4 & 123 & US & arw & sju \\ \hline 5 & 124 & US & fx & sju \\ \hline 6 & 125 & US & fx & Iny \\ \hline \end{tabular} +DF2 \begin{tabular}{|l|l|l|l|l|} \hline & P & C & S & Loc \\ \hline 1 & 120 & IN & arw & law \\ \hline 2 & 121 & US & arw & sju \\ \hline 3 & 122 & CA & arw & sju \\ \hline 4 & 123 & US & arw & sju \\ \hline 5 & 124 & US & fx & sju \\ \hline 6 & 125 & US & fx & \\ \hline \end{tabular} DF3 \begin{tabular}{|l|l|l|l|l|l|} \hline TY & Rec & p & c & s & Loc \\ \hline Base & 2 & 121 & US & arw & Iny \\ \hline Comp & 2 & 121 & US & arw & sju \\ \hline Dif & 2 & 121 & US & arw & xxx \\ \hline Base & 3 & 122 & US & arw & \\ \hline Comp & 3 & 122 & US & arw & sju \\ \hline \end{tabular}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!