【发布时间】:2019-07-07 22:43:40
【问题描述】:
我正在尝试从数据框中的一列中获取唯一标识符/键,并将其附加到另一个数据框中,并添加到两列中相同的元素集。两个数据框如下:
df1 df2
Geogkey Brand Week date Impressions
TMZ43434 x 6/16/18 6/14/18 798798
KRO36783 y 6/16/18 6/21/18 562314
.... 6/28/18 462534
n
df2 中还有几个日期直到 8 月,但为了简洁起见,我没有将它们包括在内。我想从 df1 获取每个唯一的 Geogkey 并将它们附加到 df2,因此特定日期和印象的每一行都与一个 geogkey 匹配。 df2 中的一组日期和展示次数将不断重复数据帧,每个组合对应一个唯一的 geogkey - 每次也会重复。所以最终的数据框看起来像这样:
Geogkey date Impressions
TMZ43434 6/14/18 798798
TMZ43434 6/21/18 562314
TMZ43434 6/28/18 462534
KRO36783 6/14/18 798798
KRO36783 6/21/18 562314
KRO36783 6/28/18 462534
这将针对每个 geogkey 不断重复。我到目前为止的代码是:
empty <- data.frame(df2$date, df2$impressions)
#creates a new data frame with unique geogkeys
geogname <- unique(data.frame(df1$GEOGKEY))
#create some function that will index each unique geogkey and make a new
column for df2 with that name (e.g. df2$geogkey <- some function)
new_df <- rbind(empty, df2)
#this should theoretically append all the geogkeys to the dates and
impressions
我需要为此编写一些 for 循环吗?我被卡住了,不知道如何继续。我也在尝试在 Pandas 中这样做。
【问题讨论】:
标签: python r pandas dataframe append