简体   繁体   English

基于两列创建新变量作为索引一列作为新变量名称python pandas或R.

[英]Creating new variables based on two columns as index one column as new variable names python pandas or R

Please help me edit the title if there's a better phrasing once you read the question. 如果您在阅读问题后有更好的措辞,请帮我编辑标题。

I have data that looks like this: 我的数据看起来像这样:

Location    Date    Item    Price
 12           1       A       1
 12           2       A       2      
 12           3       A       4
 13           1       A       1
 13           2       A       4
 12           1       B       1
 12           2       B       8
 13           1       B       1
 13           2       B       2
 13           3       B       11

I want to use location and date to create a new variable for each item that is that items price, for example, the output I want is: 我想使用位置和日期为每个项目创建一个新变量,即项目价格,例如,我想要的输出是:

Location    Date    PriceA   PriceB
 12           1       1       1
 12           2       2       8      
 12           3       4       NaN
 13           1       1       1
 13           2       4       2
 13           3       NaN     11

You could try reshape from base R 你可以尝试从base R reshape

 reshape(df, idvar=c('Location', 'Date'), timevar='Item', direction='wide')
 #    Location Date Price.A Price.B
 #1        12    1       1       1
 #2        12    2       2       8
 #3        12    3       4      NA
 #4        13    1       1       1
 #5        13    2       4       2
 #10       13    3      NA      11

Or 要么

library(reshape2)
dcast(df, Location+Date~paste0('Price',Item), value.var='Price')
#    Location Date PriceA PriceB
#1       12    1      1      1
#2       12    2      2      8
#3       12    3      4     NA
#4       13    1      1      1
#5       13    2      4      2
#6       13    3     NA     11

Or you could use dcast.data.table (would be faster) after converting to data.table 或者你可以在转换为data.table后使用dcast.data.table (会更快)

library(data.table)
dcast.data.table(setDT(df)[,Item:=paste0('Price', Item)],
                                         ...~Item, value.var='Price')

Or 要么

library(tidyr)
library(dplyr)
spread(df, Item, Price) %>%
                      rename(PriceA=A, PriceB=B)
#   Location Date PriceA PriceB
#1       12    1      1      1
#2       12    2      2      8
#3       12    3      4     NA
#4       13    1      1      1
#5       13    2      4      2
#6       13    3     NA     11

Update 更新

If you don't need Price as prefix, just do: 如果您不需要Price作为前缀,只需执行以下操作:

dcast.data.table(setDT(df), ...~Item, value.var='Price')

and the reshape2 option would be reshape2选项将是

dcast(df,...~Item, value.var='Price')

data 数据

df <- structure(list(Location = c(12L, 12L, 12L, 13L, 13L, 12L, 12L, 
13L, 13L, 13L), Date = c(1L, 2L, 3L, 1L, 2L, 1L, 2L, 1L, 2L, 
3L), Item = c("A", "A", "A", "A", "A", "B", "B", "B", "B", "B"
), Price = c(1L, 2L, 4L, 1L, 4L, 1L, 8L, 1L, 2L, 11L)), .Names = c("Location", 
"Date", "Item", "Price"), class = "data.frame", row.names = c(NA, 
-10L))

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

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