简体   繁体   English

如何合并 2 dataframe,创建只出现在第二个 dataframe 但不出现在第一个的行和 groupby 求和?

[英]How to combine 2 dataframe, create a row that appear only in the second dataframe but not in the 1st and groupby to get the sum?

I want to combine 2 dataframes.我想合并 2 个数据帧。 I have tried several methods but not sure how I can achieve the final dataframe. Appreciate any advice on how can i do this.我尝试了几种方法,但不确定如何实现最终的 dataframe。感谢任何有关如何执行此操作的建议。

data_list_1 = [['Employee', 'Course Name', 'Status'],
              ['Abel', "Course_A", "Completed"],
              ['Bain', "Course_A", "Incomplete"]]

data_list_2 = [['Employee', 'Course Name', 'Lesson Name', 'Lesson Score', 'Status'],
              ['Abel', 'Course_B', 'Lesson_1', 100, ""],
              ['Abel', 'Course_B', 'Lesson_2', 100, ""],
              ['Abel', 'Course_B', 'Lesson_3', 100, ""],
              ['Abel', 'Course_B', 'Lesson_4', 100, ""],
              ['Bain', 'Course_B', 'Lesson_1', 100, ""],
              ['Bain', 'Course_B', 'Lesson_2', 100, ""],
              ['Coot', 'Course_B', 'Lesson_1', 100, ""],
              ['Coot', 'Course_B', 'Lesson_2', 100, ""],
              ['Coot', 'Course_B', 'Lesson_3', 100, ""],
              ['Coot', 'Course_B', 'Lesson_4', 100, ""],
              ['Coot', 'Course_B', 'Lesson_5', 100, ""]]

Course_A_df = pd.DataFrame(data_list_1[1:], columns = data_list_1[0])
Course_B_df = pd.DataFrame(data_list_2[1:], columns = data_list_2[0])

I want to have the following dataframe to use it in Tableau for visualisation purpose.我想要以下 dataframe 在 Tableau 中将其用于可视化目的。 Basically the final df should also have Coot with None values and for Course_B Status to be completed if all 5 Lesson score is 100.基本上最终的 df 也应该有 Coot with None 值,并且如果所有 5 课的分数都是 100,则 Course_B 状态将完成。

to_achieved = [['Employee', 'Course Name', 'Lesson Name', 'Lesson Score', 'Status'],
              ['Abel', "Course_A", None, None, "Completed"],
              ['Bain', "Course_A", None, None, "Incomplete"],
              ['Coot', "Course_A", None, None, None],              
              ['Abel', 'Course_B', 'Lesson_1', 100, "Incomplete"],
              ['Abel', 'Course_B', 'Lesson_2', 100, "Incomplete"],
              ['Abel', 'Course_B', 'Lesson_3', 100, "Incomplete"],
              ['Abel', 'Course_B', 'Lesson_4', 100, "Incomplete"],
              ['Bain', 'Course_B', 'Lesson_1', 100, "Incomplete"],
              ['Bain', 'Course_B', 'Lesson_2', 100, "Incomplete"],
              ['Coot', 'Course_B', 'Lesson_1', 100, "Completed"],
              ['Coot', 'Course_B', 'Lesson_2', 100, "Completed"],
              ['Coot', 'Course_B', 'Lesson_3', 100, "Completed"],
              ['Coot', 'Course_B', 'Lesson_4', 100, "Completed"],
              ['Coot', 'Course_B', 'Lesson_5', 100, "Completed"]]

to_achieved_df = pd.DataFrame(to_achieved[1:], columns = to_achieved[0])
to_achieved_df

I have tried concat and merge but it doesn't seems to give me what i want.我试过 concat 和 merge 但它似乎没有给我我想要的东西。

df_concat = pd.concat([Course_A_df, Course_B_df], axis=0, ignore_index=True)
df_concat
merged = pd.merge(left=Course_A_df, right=Course_B_df, left_on='Employee', right_on='Employee', how='left')
merged

For the calculation of status, i have tried groupby, but is that any way i can check if the value is 500 and update the status?对于状态的计算,我已经尝试过 groupby,但是我可以通过这种方式检查值是否为 500 并更新状态吗?

Thank you!谢谢!

You can .reindex Course_A_df to add missing Employees:您可以.reindex Course_A_df添加缺少的员工:

Course_A_df = (
    Course_A_df.set_index("Employee")
    .reindex(Course_B_df["Employee"].unique())
    .reset_index()
)
Course_A_df["Course Name"] = Course_A_df["Course Name"].ffill().bfill()

Prints:印刷:

  Employee Course Name      Status
0     Abel    Course_A   Completed
1     Bain    Course_A  Incomplete
2     Coot    Course_A         NaN

Then add "Status" column to Course_B_df :然后将“状态”列添加到Course_B_df

Course_B_df["Status"] = Course_B_df.groupby(
    ["Employee", "Course Name"], as_index=False
)["Lesson Score"].transform(
    lambda x: "Complete" if x.sum() == 500 else "Incomplete"
)

Prints:印刷:

   Employee Course Name Lesson Name  Lesson Score      Status
0      Abel    Course_B    Lesson_1           100  Incomplete
1      Abel    Course_B    Lesson_2           100  Incomplete
2      Abel    Course_B    Lesson_3           100  Incomplete
3      Abel    Course_B    Lesson_4           100  Incomplete
4      Bain    Course_B    Lesson_1           100  Incomplete
5      Bain    Course_B    Lesson_2           100  Incomplete
6      Coot    Course_B    Lesson_1           100    Complete
7      Coot    Course_B    Lesson_2           100    Complete
8      Coot    Course_B    Lesson_3           100    Complete
9      Coot    Course_B    Lesson_4           100    Complete
10     Coot    Course_B    Lesson_5           100    Complete

and finally .concat the two:最后.concat两者:

out = pd.concat([Course_A_df, Course_B_df])
print(out[["Employee", "Course Name", "Lesson Name", "Lesson Score", "Status"]])

Prints:印刷:

   Employee Course Name Lesson Name  Lesson Score      Status
0      Abel    Course_A         NaN           NaN   Completed
1      Bain    Course_A         NaN           NaN  Incomplete
2      Coot    Course_A         NaN           NaN         NaN
0      Abel    Course_B    Lesson_1         100.0  Incomplete
1      Abel    Course_B    Lesson_2         100.0  Incomplete
2      Abel    Course_B    Lesson_3         100.0  Incomplete
3      Abel    Course_B    Lesson_4         100.0  Incomplete
4      Bain    Course_B    Lesson_1         100.0  Incomplete
5      Bain    Course_B    Lesson_2         100.0  Incomplete
6      Coot    Course_B    Lesson_1         100.0    Complete
7      Coot    Course_B    Lesson_2         100.0    Complete
8      Coot    Course_B    Lesson_3         100.0    Complete
9      Coot    Course_B    Lesson_4         100.0    Complete
10     Coot    Course_B    Lesson_5         100.0    Complete

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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