how can I sum the values in column \'two\' based on the items in column \'one\' in pandas dataframe:
df = pd.DataFrame({\'One\': [\'A\', \'B\', \'A\', \'B\'], \'
The trick is use pandas built-in functions .groupby(COLUMN_NAME) and then .sum() that new pandas object
.groupby(COLUMN_NAME)
.sum()
import pandas as pd df = pd.DataFrame({'One': ['A', 'B', 'A', 'B'], 'Two': [1, 5, 3, 4]}) groups = df.groupby('One').sum() print(groups.head())