0

I have a dataframe that looks something like this:

    Row-Labels   300    301
0   BLR          1
1   BLR          2      2
2   NSL         
3   RFR          3
4   TNC          22

What I want to get is something like this:

    Row-Labels   300    301
0   BLR          3      2
1   NSL        
2   RFR          3
3   TNC          22

That is, I want to merge all rows into one where row-labels column values are same. This merge should be such that the column in the resulting row should sum all values in column A for row-labels value 'BLR' and put other values as it is in the that column of the row if no sum is required.

What I have done so far is something like this:

for i in range(300, 301):
    temp_final_df.append(final_df.groupby(['Row-Labels'])['{}'.format(i)].apply(sum).reset_index())

But its output is not correct. In short, I want to merge rows such that Row_labels column values become unique. Any other ideas as how can I achieve this?

rootkit
  • 353
  • 1
  • 3
  • 15

1 Answers1

0

use:

df.groupby('Row-Labels').sum().replace(0,np.nan)

if you dont want np.nan and just blank values:

df.groupby('Row-Labels').sum().replace(0,'')

Edit:

df.groupby('Row-Labels').sum().replace(0,'').reset_index()

output:

    Row-Labels   300     301
0   BLR          3       2 
1   NSL   
2   RFR          3  
3   TNC          22 
anky
  • 74,114
  • 11
  • 41
  • 70
  • this groups the 'Row-Labels' together but values in columns are empty. Meaning, the row values for 'Row-Labels' column are as I want them but columns 300 and 301 are empty – rootkit Sep 10 '18 at 12:53
  • @rootkit : i have added the output too.pl check if it helps. Thanks – anky Sep 10 '18 at 13:05