How to concat Pandas dataframe columns

ArchieTiger

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
EdChum

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

concat MultiIndex pandas DataFrame columns

From Java

How to concat multiple Pandas DataFrame columns with different token separator?

From Dev

How to concat multiple Pandas DataFrame columns with different token separator?

From Dev

pandas dataframe concat is giving unwanted NA/NaN columns

From Dev

How to reduce columns in dataframe pandas

From Dev

concat a DataFrame with a Series in Pandas

From Dev

scala - how to concatenate columns of a DataFrame with concat_ws?

From Dev

Pandas CONCAT() with merged columns in Creation

From Dev

how to concat sets when using groupby in pandas dataframe?

From Dev

How can I avoid repeated indices in pandas DataFrame after concat?

From Dev

How to concat two columns

From Java

How to show all of columns name on pandas dataframe?

From Dev

how to split 'number' to separate columns in pandas DataFrame

From Java

How to GroupBy a Dataframe in Pandas and keep Columns

From Java

pandas how to check dtype for all columns in a dataframe?

From Dev

how to add columns label on a Pandas DataFrame

From Dev

how to switch columns rows in a pandas dataframe

From Dev

How to filter pandas dataframe columns by partial label

From Dev

How to reorder values across columns in a pandas DataFrame?

From Dev

How to concatenate pandas dataframe columns into iterable lists?

From Dev

How to split a pandas dataframe into multiple columns

From Dev

How to name Pandas Dataframe Columns automatically?

From Dev

How to sort pandas dataframe by two date columns

From Dev

How to sort pandas dataframe by two date columns

From Dev

How to sort pandas dataframe by two date columns

From Dev

How to add columns to an empty pandas dataframe?

From Dev

How to show all of columns name on pandas dataframe?

From Dev

pandas how to check dtype for all columns in a dataframe?

From Dev

How to populate Pandas dataframe as function of index and columns

Related Related

  1. 1

    concat MultiIndex pandas DataFrame columns

  2. 2

    How to concat multiple Pandas DataFrame columns with different token separator?

  3. 3

    How to concat multiple Pandas DataFrame columns with different token separator?

  4. 4

    pandas dataframe concat is giving unwanted NA/NaN columns

  5. 5

    How to reduce columns in dataframe pandas

  6. 6

    concat a DataFrame with a Series in Pandas

  7. 7

    scala - how to concatenate columns of a DataFrame with concat_ws?

  8. 8

    Pandas CONCAT() with merged columns in Creation

  9. 9

    how to concat sets when using groupby in pandas dataframe?

  10. 10

    How can I avoid repeated indices in pandas DataFrame after concat?

  11. 11

    How to concat two columns

  12. 12

    How to show all of columns name on pandas dataframe?

  13. 13

    how to split 'number' to separate columns in pandas DataFrame

  14. 14

    How to GroupBy a Dataframe in Pandas and keep Columns

  15. 15

    pandas how to check dtype for all columns in a dataframe?

  16. 16

    how to add columns label on a Pandas DataFrame

  17. 17

    how to switch columns rows in a pandas dataframe

  18. 18

    How to filter pandas dataframe columns by partial label

  19. 19

    How to reorder values across columns in a pandas DataFrame?

  20. 20

    How to concatenate pandas dataframe columns into iterable lists?

  21. 21

    How to split a pandas dataframe into multiple columns

  22. 22

    How to name Pandas Dataframe Columns automatically?

  23. 23

    How to sort pandas dataframe by two date columns

  24. 24

    How to sort pandas dataframe by two date columns

  25. 25

    How to sort pandas dataframe by two date columns

  26. 26

    How to add columns to an empty pandas dataframe?

  27. 27

    How to show all of columns name on pandas dataframe?

  28. 28

    pandas how to check dtype for all columns in a dataframe?

  29. 29

    How to populate Pandas dataframe as function of index and columns

HotTag

Archive