0

i have a question. Given a pandas dataframe, is it possible to find similar strings in the column Letters such as if Letters=a, the corresponding values are summed up.(So the total if Letters=a is 3500 and Letters=b is 11200) without doing it like this?

import pandas as pd

data = {'Letters': ["a","b","b","a"],
        'Values': [1500,2200,9000,2000]
        }

df = pd.DataFrame(data,columns=['Letters',"Values"])
totala=0
totalb=0
print()
for i in range(len(df)):
  if df.Letters[i]=="a":
    totala+=df.Values[i]
  else:
    totalb+=df.Values[i]


print(totala)
print(totalb)

Thanks

brezz
  • 65
  • 4

0 Answers0