简体   繁体   English

Left Outer在数据框上按键连接具有Seri​​es对象的数据框,在系列上索引

[英]Left Outer Join a Data frame with a Series object by key on data frame, index on series

Is it possible to join a series object to a dataframe without having to turn the series into a dataframe? 是否可以将系列对象连接到数据框而无需将系列转换为数据框?

Currently, I calculate something, get a series as a result, and have to turn the series into a dataframe to merge the two: 目前,我计算一些东西,得到一个系列作为结果,并且必须将系列变成数据帧以合并两者:

clicked_series = p.clicked.sum();
temp_df = pd.DataFrame({'ad_id':clicked_series.index, 'clicks':clicked_series.values})
full_df = pd.merge(full_df, temp_df, on='ad_id', how='left')

Is it possible to conduct a left outer join on the series and dataframe directly, without having to create a temporary data frame? 是否可以直接在系列和数据框上进行左外连接,而无需创建临时数据框?

use reindex 使用reindex

full_df['clicks'] = clicked_series.reindex(full_df.ad_id).values

old answers 老答案

Use join 使用join
technically, I'm still converting to a pd.DataFrame but... 从技术上讲,我还在转换为pd.DataFrame但......

clicked_series = p.clicked.sum();
full_df = full_df.join(clicked_series.to_frame('clicks'), on='ad_id', how='left')

Another option is to use pd.concat . 另一种选择是使用pd.concat But this will look like an outer join. 但这看起来像是外连接。

pd.concat([full_df.set_index('ad_id'),
           clicked_series.rename('clicks')], axis=1).reset_index()

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

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