lignin
lignin

Reputation: 459

Pandas Dataframe merge 2 columns

I have a datatable like this:

Run, test1, test2
1, 100, 102.
2, 110, 100.
3, 108, 105.

I would like to have the 2 columns merged together like this:

Run, results
1, 100
1, 102
2, 110 
2, 100 
3, 108 
3, 105

How do I do it in Pandas? Thanks a lot!

Upvotes: 2

Views: 363

Answers (2)

BENY
BENY

Reputation: 323226

This how I achieve this

Option 1

wide_to_long

pd.wide_to_long(df,stubnames='test',i='Run',j='LOL').reset_index().drop('LOL',1)
Out[776]: 
    Run   test
0    1  100.0
1    2  110.0
2    3  108.0
3    1  102.0
4    2  100.0
5    3  105.0

Notice : Here I did not change the column name from test to results, I think by using test as new column name is better in your situation .


Option 2

pd.concat

df=df.set_index('Run')
pd.concat([df[Col] for Col in df.columns],axis=0).reset_index().rename(columns={0:'results'})

Out[786]: 
   Run  results
0    1    100.0
1    2    110.0
2    3    108.0
3    1    102.0
4    2    100.0
5    3    105.0

Upvotes: 2

jezrael
jezrael

Reputation: 862581

Use stack with Multiindex to column by double reset_index:

df = df.set_index('Run').stack().reset_index(drop=True, level=1).reset_index(name='results')
print (df)
   Run  results
0    1    100.0
1    1    102.0
2    2    110.0
3    2    100.0
4    3    108.0
5    3    105.0

Or melt:

df = df.melt('Run', value_name='results').drop('variable', axis=1).sort_values('Run')
print (df)
   Run  results
0    1    100.0
3    1    102.0
1    2    110.0
4    2    100.0
2    3    108.0
5    3    105.0

Numpy solution with numpy.repeat:

a = np.repeat(df['Run'].values, 2)
b = df[['test1','test2']].values.flatten()

df = pd.DataFrame({'Run':a , 'results': b}, columns=['Run','results'])
print (df)
   Run  results
0    1    100.0
1    1    102.0
2    2    110.0
3    2    100.0
4    3    108.0
5    3    105.0

Upvotes: 2

Related Questions