1

I have a huge tab-delimited file such as the following :

3 Line1 0 100 A
4 Line1 100 200 A
7 Line1 200 300 B
2 Line1 300 400 B
12 Line1 400 500 C
10 Line1 500 600 C

For all the rows that have the letters (A, B, ect), I need to combine their values based upon the number in the first column. For example, what should be the result is below:

7 A
9 B
22 C

I am currently using Pandas + Python to figure this out.

Steveman30290
  • 511
  • 7
  • 17

3 Answers3

1

Suppose the df is as below:

   val     id  line  col1 col2
0    3  Line1     0   100    A
1    4  Line1   100   200    A
2    7  Line1   200   300    B
3    2  Line1   300   400    B
4   12  Line1   400   500    C
5   10  Line1   500   600    C

Then, I think you can use groupby followed by sum:

result_df = df.groupby('col2')['val'].sum().to_frame('Sum')
print(result_df)

Result:

      Sum
col2     
A       7
B       9
C      22
niraj
  • 17,498
  • 4
  • 33
  • 48
0

You have to use join() method

Table1.join(table2.set_index(''key"),on='key')
Julio CamPlaz
  • 857
  • 8
  • 18
0
df = pd.DataFrame({'Col1':[3,4,7,2,12,10],'Col2':['A','A','B','B','C','C']})
df.groupby('Col2').sum()   
It_is_Chris
  • 13,504
  • 2
  • 23
  • 41