1

I've a panda dataframe in this format.

   Var1                  Var2                        Var2
    A   [2016-09-26, 2016-08-25, 2016-08-06] [u'1', u'5', u'4']
    B   [2016-09-26, 2016-08-25, 2016-08-06] [u'1', u'5', u'4']

I want to convert it into the following format.

Var1     Var2      Var3
 A    2016-09-26    1
 A    2016-08-25    5
 A    2016-08-06    4
 B    2016-09-26    1
 B    2016-08-25    5
 B    2016-08-06    4

Can anyone please tell me how to do it?

Thanks a lot!

1 Answer 1

1

If values in columns Var2 and Var3 are in lists, you can use numpy.repeat for repeat values by legths by str.len and flat values of nested lists by chain:

print (type(df.Var2.iat[0]))
<class 'list'>

print (type(df.Var3.iat[0]))
<class 'list'>

from  itertools import chain

df1 = pd.DataFrame({
        "Var1": np.repeat(df.Var1.values, df.Var2.str.len()),
        "Var2": list(chain.from_iterable(df.Var2)),
        "Var3": list(chain.from_iterable(df.Var3))})

print (df1)
  Var1        Var2 Var3
0    A  2016-09-26    1
1    A  2016-08-25    5
2    A  2016-08-06    4
3    B  2016-09-26    1
4    B  2016-08-25    5
5    B  2016-08-06    4
Sign up to request clarification or add additional context in comments.

2 Comments

@jazrael: Thanks a lot! This answer perfectly solved my problem.
Super, glad can help you. And nice day!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.