You can use pd.concat with GroupBy.agg
# df = pd.DataFrame({'Movie':['IR', 'R'], 'rating':[95, 90], 'director':['SB', 'RC']})
# df1 = pd.DataFrame({'Movie':['IR', 'BH'], 'rating':[93, 88], 'direction':['SB', 'RC']})
(pd.concat([df, df1]).groupby('Movie', as_index=False).
agg({'rating':'mean', 'director':'first'}))
Movie rating director
0 BH 88 RC
1 IR 94 SB
2 R 90 RC
Or df.append
df.append(df1).groupby('Movie',as_index=False).agg({'rating':'mean', 'director':'first'})
Movie rating director
0 BH 88 RC
1 IR 94 SB
2 R 90 RC
- If you want
Movie column as index, as_index parameter of df.groupby defaults to True, Movie column would be index, remove as_index=False from groupby
- If you want to maintain the order then set
sort parameter to Truein groupby.
(df.append(df1).groupby('Movie',as_index=False, sort=False).
agg({'rating':'mean', 'director':'first'}))
Movie rating director
0 IR 94 SB
1 R 90 RC
2 BH 88 RC