2

I would like to combine rows of the same dataframe, more precisely, to take rows that have the same values in a specific column and create only one row. Here is an example:

I have the following dataframe:

te= {'TEAM': ['HC','TC','HC','BC','TC','BC'],
        'A1': [22,25,27,35,31,41],
        'A2': [20,50,70,11,14,12]
        }

df = pd.DataFrame(te,columns= ['TEAM', 'A1', "A2"])

print (df)

 TEAM  A1  A2
0   HC  22  20
1   TC  25  50
2   HC  27  70
3   BC  35  11
4   TC  31  14
5   BC  41  12

and I would like to form a row for the three possibles values of the column TEAM such as the expected output look like this:

 TEAM  A1  A2  A1(1)  A2(1)
0   HC  22  20     27     70
1   TC  25  50     31     14
2   BC  35  11     41     12

How can I do that ?

1
  • 2
    the last row of your output isnt correct. it should be 35 11 41 12 Commented Nov 13, 2019 at 6:18

2 Answers 2

1

It is pivot table with pre-process for columns

s = df.groupby('TEAM').cumcount()
m = s.astype(bool) * ('('+s.astype(str)+')')
df_out = df.set_index(['TEAM', m]).unstack().sort_index(level=1, axis=1).reset_index()
df_out.columns = df_out.columns.map(lambda x: f'{x[0]}{x[1]}')

Out[268]:
  TEAM  A1  A2  A1(1)  A2(1)
0   BC  35  11     41     12
1   HC  22  20     27     70
2   TC  25  50     31     14
Sign up to request clarification or add additional context in comments.

Comments

0

There may be a better way, but this solution scales to an arbitrary number of lines.

df['order'] = df.groupby('TEAM').cumcount() + 1
df.set_index(['TEAM','order']).unstack()
#       A1      A2         
#order   1   2   1   2  
#TEAM                       
#BC     35  41  11  12  
#HC     22  27  20  70  
#TC     25  31  50  14  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.