How do I convert the following dataframe from df1 to df2?
JavaScript
x
19
19
1
df1 = pd.DataFrame([['a b c', '1 2 3', 2011], ['d e f', '4 5 6', 2012]])
2
#df1
3
a b c
4
0 a b c 1 2 3 2011
5
1 d e f 4 5 6 2012
6
7
8
df2 = pd.DataFrame([['a', 1, 2011], ['b', 2, 2011], ['c', 3, 2011],
9
['d', 4, 2012], ['e', 5, 2012], ['f', 6, 2012]])
10
11
#df2
12
a b c
13
0 a 1 2011
14
1 b 2 2011
15
2 c 3 2011
16
3 d 4 2012
17
4 e 5 2012
18
5 f 6 2012
19
Thanks in advance!
Advertisement
Answer
You could try as follows. Apply .str.split
to columns a
and b
in a loop, each time exploding
the result. This will get us 2 pd.Series
and we use pd.concat
to put them together. After this, we just need to assign col c
, and reset the index. So:
JavaScript
1
24
24
1
import pandas as pd
2
3
# adding columns=['a','b','c'] to match data in your post:
4
df1 = pd.DataFrame([['a b c', '1 2 3', 2011], ['d e f', '4 5 6', 2012]], columns=['a','b','c'])
5
6
res = pd.concat([df1[col].str.split(' ').explode() for col in ['a','b']], axis=1)
7
8
# change dtype for second col
9
res['b'] = res['b'].astype(int)
10
11
# assign column `c` and finally, reset index
12
res['c'] = df1['c']
13
res.reset_index(drop=True,inplace=True)
14
15
print(res)
16
17
a b c
18
0 a 1 2011
19
1 b 2 2011
20
2 c 3 2011
21
3 d 4 2012
22
4 e 5 2012
23
5 f 6 2012
24