Friday, November 11, 2022

De-compress customer record into multiple rows

De-compressing Rows


Introduction

In this blog we will we can de-compress aggregated customer records.

package.name<-c("dplyr","stringr")

for(i in package.name){

  if(!require(i,character.only = T)){

    install.packages(i)
  }
  library(i,character.only = T)

}


Step 1: Creating a data frame

Lets create a dummy data frame with compressed shopping history for a customer.

df<-data.frame(Customer_Num="Customer 1",
               Product_Name=c("A|B|AB|C|X")
               )
head(df)
  Customer_Num Product_Name
1   Customer 1   A|B|AB|C|X

Just by looking at the data we cant get a hang of whats happening.What we require here is a granular view of things. Maybe something like the below might give us better idea.


Now lets try and get the same summary using dplyr and toString

Step 2: Creating granular records at Customer Level

l1<-list()

l1<-lapply(1:nrow(df), function(x){
  
  
  x2<-str_split(df[x,"Product_Name"],"['|]")[[1]]
  interim.df<-data.frame(Customer = df[x,"Customer_Num"],
             Product = x2)
  return(interim.df)
  
  
})


final.df<-do.call(rbind.data.frame,l1)
final.df
    Customer Product
1 Customer 1       A
2 Customer 1       B
3 Customer 1      AB
4 Customer 1       C
5 Customer 1       X


My Youtube Channel

Web Scraping Tutorial 4- Getting the busy information data from Popular time page from Google

Popular Times Popular Times In this blog we will try to scrape the ...