2

How to concat columns of different data types in a Pandas dataframe such that if column number is concatenated with column operator, and I do a groupby('user').sum(), I can have the appropriate aggregation:

    number   operator user
id                      
1      193        -    A
2      332        +    B
3     4434        +    A
4      432        -    C
5      652        +    C
6      567        +    D


#after concat:

    number operator user
id                      
1     -193        -    A
2      332        +    B
3     4434        +    A
4     -432        -    C
5      652        +    C
6      567        +    D


#df.groupby('user').sum()

      number
user        
A       4241
B        332
C        220
D        567

1 Answer 1

2

Use loc with boolean mask to make the 'number' values negative:

In [34]:
df.loc[df['operator'] == '-', 'number'] = -df['number']
df

Out[34]:
    number operator user
id                      
1     -193        -    A
2      332        +    B
3     4434        +    A
4     -432        -    C
5      652        +    C
6      567        +    D

You can then groupby on 'user' can call sum on 'number' column:

In [35]:    
df.groupby('user')['number'].sum()

Out[35]:
user
A    4241
B     332
C     220
D     567
Name: number, dtype: int64
Sign up to request clarification or add additional context in comments.

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.