1
我想合併/連接兩個數據幀,每個數據幀都有三個鍵(Age,Gender和Signed_In)。兩個數據框都具有相同的父級,並由groupby創建,但具有唯一的值列。Python:合併/連接兩個數據幀
鑑於獨特的組合鍵在兩個數據框之間共享,似乎合併/連接應該是無痛的。想到那裏,我想嘗試'合併'和'加入',但是不能在我的生活中解決它。
times = pd.read_csv('nytimes.csv')
# Produces times_mean table consisting of two value columns, avg_impressions and avg_clicks
times_mean = times.groupby(['Age','Gender','Signed_In']).mean()
times_mean.columns = ['avg_impressions', 'avg_clicks']
# Produces times_max table consisting of two value columns, max_impressions and max_clicks
times_max = times.groupby(['Age','Gender','Signed_In']).max()
times_max.columns = ['max_impressions', 'max_clicks']
# Following intended to produce combined table with four value columns
times_join = times_mean.join(times_max, on = ['Age', 'Gender', 'Signed_In'])
times_join2 = pd.merge(times_mean, times_max, on=['Age', 'Gender', 'Signed_In'])
我們如果沒有'nytimes.csv'就無法測試。我的猜測是,既然''年齡'','性別','Signed_In''是指數,你也不需要'加入'' –
'的調用,你應該提供什麼錯誤。 –
欣賞筆記,我第一次發佈 - 絕對應該包含原始文件。 – jamesbev