Pandas Dataframe: Split Multiple Columns Into Multiple Columns
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
Solution 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
Post a Comment for "Pandas Dataframe: Split Multiple Columns Into Multiple Columns"