Reputation: 207
I have this dataframe:
0 1
0 Bin Months Since Default
1 1 0
2 2 0< x <=6
3 3 6< x <=12
4 4 12< x <=24
5 5 24<
I want to create a new column 'texts' that will concatenate both 0 and 1 one below the other so that the resultant dataframe looks like this:
0 1 texts
0 Bin Months Since Default Bin
1 1 0 1
2 2 0< x <=6 2
3 3 6< x <=12 3
4 4 12< x <=24 4
5 5 24< 5
6 NaN NaN Months Since Default
7 NaN NaN 0< x <=6
8 NaN NaN 6< x <=12
9 NaN NaN 12< x <=24
10 NaN NaN 24<
Is it possible?
Upvotes: 2
Views: 1508
Reputation: 862541
Use DataFrame.melt
with concat
:
#by all columns
s = df.melt(value_name='texts')['texts']
#if need filter columns by list
#s = df[[0,1]].melt(value_name='texts')['texts']
df = pd.concat([df, s], axis=1)
print (df)
0 1 texts
0 Bin Months Since Default Bin
1 1 0 1
2 2 0< x <=6 2
3 3 6< x <=12 3
4 4 12< x <=24 4
5 5 24< 5
6 NaN NaN Months Since Default
7 NaN NaN 0
8 NaN NaN 0< x <=6
9 NaN NaN 6< x <=12
10 NaN NaN 12< x <=24
11 NaN NaN 24<
Upvotes: 1