0

I have 2 series

s1 = pd.Series(["val1","val2","val3"],index=["var1","var2","var3"],name="one")
s2 = pd.Series(["A","B","C"],name="two")

my final goal is to obtain a dataframe like this (vars are the columns, index is ommited):

var1 var2 var3 var4
val1 val2 val3 A
val1 val2 val3 B
val1 val2 val3 C

Is there any way to achieve this in an easy manner?

More generally, suppose I have a dataframe like this (read -Note 2- below)

      var1  var2  var3
   1 val11 val12  A,B,C
   2 val21 val22  D,E,F,G,H
   ........................

how can I obtain the following dataframe?

var1  var2 var3
val11 val12    A
val11 val12    B
val11 val12    C
val21 val22    D
val21 val22    E
val21 val22    F
val21 val22    G
val21 val22    H
................

Note1: here all values (val1,var1,A...) can be thought as strings that contain a value equal to their name ("val1","var1","A"). Note2: "," in the var3 column for the general problem is just some kind of string concatenation. E.g.: the string "A,B,C" and "D,E,F,G,H". Essentially its a string containing the values that I should retrieve with whatever function is needed (in this case you could use split(","))

anky
  • 74,114
  • 11
  • 41
  • 70
user57284
  • 62
  • 8

0 Answers0