I have a DataFrame as follows,
import pandas as pd
df = pd.DataFrame({'sent_a.1': [0, 3, 2, 1],
'sent_a.2': [0, 1, 4, 0],
'sent_b.3': [0, 6, 0, 8],
'sent_b.4': [1, 1, 8, 6]
})
I want to combine the columns that share a name. so the desired output would be,
sent_a sent_b
0 0,0 0,1
1 3,1 6,1
2 2,4 0,8
3 1,0 8,6
I have done the following and the code works, but since my original DataFrame has more than 200 columns, I am looking for a faster way of doing that.
df['sent_a'] = df['sent_a.1']+','+ df['sent_a.2']
df['sent_b'] = df['sent_b.1']+','+ df['sent_b.2']
df = df.drop(['sent_a.1', 'sent_a.2', 'sent_b.3', 'sent_b.4'], axis=1)