【发布时间】:2014-03-23 18:11:55
【问题描述】:
我正在尝试合并/加入两个数据框,每个数据框都有三个键(Age、Gender 和 Signed_In)。两个数据框具有相同的父级,并且由 groupby 创建,但具有唯一值列。
鉴于唯一的组合键在两个数据帧之间共享,看来合并/连接应该是轻松的。认为我尝试“合并”和“加入”一定有一些简单的错误,但我终生无法解决。
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,我们将无法对此进行测试。我的猜测是,由于'Age', 'Gender', 'Signed_In'是索引,因此在调用join时不需要onkwarg -
另外,你应该提供错误是什么。
-
欣赏笔记,我第一次发帖——绝对应该包含原始文件。