-1

I have a variable in which column 1 is a list of fish names and column 2 is the number of that species caught. There are many repeated fish in column 1 and corresponding catch counts in column 2 (from different sampling/catch instances).

I would like to merge together repeated fish names and sum their corresponding catch counts - so that there are no repeats and each fish has a single integer pair equaling the total number caught.

Variable visualized:

Column 1 Column 2
Fish 1 10
Fish 2 5
Fish 1 5
Fish 3 1
Fish 1 5
Fish 2 3

New desired variable visualized:

Column 1 Column 2
Fish 1 20
Fish 2 8
Fish 3 1

Not sure where to begin, looking for any guidance/help.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
sf33
  • 1
  • 1
  • It depends on the end purpose that this will be used for. If there will be a lot of data processing using these then you should look into the pandas library: https://pandas.pydata.org/docs/ . Otherwise if they are stored like a dictionary then you can use the groupby function from the itertools library. https://docs.python.org/3/library/itertools.html#itertools.groupby – NTamez8 Jun 17 '22 at 14:33
  • Variables don't have columns. Do you have a pandas dataframe by any chance? Can you please provide a *code* example of the situation? – MisterMiyagi Jun 19 '22 at 18:29
  • Does this answer your question? [Python - sum values in dictionary](https://stackoverflow.com/questions/11692613/python-sum-values-in-dictionary) – MisterMiyagi Jun 19 '22 at 18:32

1 Answers1

0

First convert your data to dataframe Then

import pandas as pd

df=pd.concat([df1,df2])
df=df.groupby('column1')['column2'].sum()



Alireza75
  • 513
  • 1
  • 4
  • 19