0

Here's an image of my dataframe.

enter image description here

I am working on a dataframe that displays the amount of offices in each state. I am looking to combine the office count value in the state of CA which is split up into 2 separate rows/indexes.

4
  • 1
    Welcome to Stack Overflow. Please read how to ask good questions. Make sure your question covers these 3 elements: 1. Problem Statement 2. Your Code (it should be Minimal, Reproducible Example 3. Error Message (preferably full Traceback to help others review and provide feedback). Sometimes the same question may have already been asked. Make sure your question is not a duplicate Commented Mar 23, 2021 at 19:47
  • You can use df.groupby('state')['office_count'].sum() to get you the sum by state Commented Mar 23, 2021 at 19:48
  • Are the two index numbers 25 and 13 important for you? If yes, what do you want to do about them? If not, the above groupby will address the problem Commented Mar 23, 2021 at 19:49
  • these were the original index positions so not entirely relevant. Thank you this worked great! Commented Mar 23, 2021 at 20:03

2 Answers 2

1

You can groupby the state column and sum the office_count column.

df = df.groupby('state').agg({'office_count' : 'sum'}).reset_index()

Sign up to request clarification or add additional context in comments.

Comments

1

You can use the groupby function for this

import pandas as pd

data = {'index': [4, 41, 26, 25, 13, 47, 20],
        'state': ['AL', 'AR', 'AZ', 'CA', 'CA', 'CO', 'CT'],
        'office_count': [28, 23, 76, 408, 374, 150, 158]}

df = pd.DataFrame(data)

df.groupby(['state']).agg({'office_count': 'sum'})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.