Python Pandas: Join on unique column values and concatenate

Zhubarb

I have three Pandas dataframes, df1, df2, and df3, as below:

import pandas as pd
import numpy as np
df1 = pd.DataFrame({'id' : ['one', 'two', 'three'], 'score': [56, 45, 78]})
df2 = pd.DataFrame({'id' : ['one', 'five', 'four'], 'score': [35, 81, 90]})
df3 = pd.DataFrame({'id' : ['five', 'two', 'six'], 'score': [23, 66, 42]})

How can I join these dataframes based on id and then concatenate their columns together? The desired output is as below:

#join_and_concatenate by id:

id   score(df1)  score(df2)  score(df3)
one    56            35         NaN
two    45            NaN        66
three  78            NaN        NaN
four   NaN           90         NaN
five   NaN           81         23
six    NaN           NaN        42

I found a relevant page that talks about merge(), concatenate() and join() but I am not sure any of these give what I want.

TomAugspurger

There may be a better way with concat, but this should work:

In [48]: pd.merge(df1, df2, how='outer', on='id').merge(df3, how='outer', on='id')
Out[48]: 
      id  score_x  score_y  score
0    one       56       35    NaN
1    two       45      NaN     66
2  three       78      NaN    NaN
3   five      NaN       81     23
4   four      NaN       90    NaN
5    six      NaN      NaN     42

[6 rows x 4 columns]

To get your desired answer:

In [54]: merged = pd.merge(df1, df2, how='outer', on='id').merge(df3, how='outer', on='id')

In [55]: merged.set_index('id').rename(columns={'score_x': 'score(df1)', 'score_y': 'score(df2)
', 'score': 'score(df3)'})
Out[55]: 
       score(df1)  score(df2)  score(df3)
id                                       
one            56          35         NaN
two            45         NaN          66
three          78         NaN         NaN
five          NaN          81          23
four          NaN          90         NaN
six           NaN         NaN          42

[6 rows x 3 columns]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python Pandas: Join on unique column values and concatenate

From Dev

Python Pandas: concatenate rows with unique values

From Dev

Python Pandas: concatenate rows with unique values

From Dev

Python/Pandas: Drop duplicate rows in dataframe, concatenate values in one column

From Dev

Join unique values into new data frame (python, pandas)

From Dev

Concatenate column values in Pandas DataFrame with "NaN" values

From Dev

Concatenate column values in Pandas DataFrame with "NaN" values

From Dev

Python Pandas Unstacking Unique Column Values to Columns Of Their Own

From Dev

Python Pandas Unstacking Unique Column Values to Columns Of Their Own

From Dev

Concatenate values into unique values

From Dev

How to use join to fill missing values of a column - Python Pandas?

From Dev

How to use join to fill missing values of a column - Python Pandas?

From Dev

concatenate only unique values

From Dev

Concatenate a set of column values based on another column in Pandas

From Dev

How to convert values of a pandas data frame column to a numeric representation based on the number of unique values, all at once in Python?

From Dev

Unique values from some column, DF pandas

From Dev

Get indexes of unique values in column (pandas)

From Dev

To_CSV unique values of a pandas column

From Dev

Pandas: Replace multiple column values by unique value

From Dev

Finding intersection of values in a column associated with unique values in another column Pandas

From Dev

R matrix: unique by one column, add the values on other column and concatenate the values on other

From Dev

Concatenate two numerical values to make a new column using pandas?

From Dev

Concatenate column values

From Dev

PHP / MYSQL - display only left join items in table but concatenate right join values in a column

From Dev

python/pandas - counting unique values in a single DataFrame column and displaying counts as new columns

From Dev

Transform a 3 columns (x, y, result) Python Pandas DataFrame to a DataFrame of result values with x (unique) as row and y (unique) as column

From Dev

Pandas, append column based on unique subset of column values

From Dev

Pandas, append column based on unique subset of column values

From Dev

Creating Dictionary from Pandas DataFrame Column Based on Unique Values in Column

Related Related

  1. 1

    Python Pandas: Join on unique column values and concatenate

  2. 2

    Python Pandas: concatenate rows with unique values

  3. 3

    Python Pandas: concatenate rows with unique values

  4. 4

    Python/Pandas: Drop duplicate rows in dataframe, concatenate values in one column

  5. 5

    Join unique values into new data frame (python, pandas)

  6. 6

    Concatenate column values in Pandas DataFrame with "NaN" values

  7. 7

    Concatenate column values in Pandas DataFrame with "NaN" values

  8. 8

    Python Pandas Unstacking Unique Column Values to Columns Of Their Own

  9. 9

    Python Pandas Unstacking Unique Column Values to Columns Of Their Own

  10. 10

    Concatenate values into unique values

  11. 11

    How to use join to fill missing values of a column - Python Pandas?

  12. 12

    How to use join to fill missing values of a column - Python Pandas?

  13. 13

    concatenate only unique values

  14. 14

    Concatenate a set of column values based on another column in Pandas

  15. 15

    How to convert values of a pandas data frame column to a numeric representation based on the number of unique values, all at once in Python?

  16. 16

    Unique values from some column, DF pandas

  17. 17

    Get indexes of unique values in column (pandas)

  18. 18

    To_CSV unique values of a pandas column

  19. 19

    Pandas: Replace multiple column values by unique value

  20. 20

    Finding intersection of values in a column associated with unique values in another column Pandas

  21. 21

    R matrix: unique by one column, add the values on other column and concatenate the values on other

  22. 22

    Concatenate two numerical values to make a new column using pandas?

  23. 23

    Concatenate column values

  24. 24

    PHP / MYSQL - display only left join items in table but concatenate right join values in a column

  25. 25

    python/pandas - counting unique values in a single DataFrame column and displaying counts as new columns

  26. 26

    Transform a 3 columns (x, y, result) Python Pandas DataFrame to a DataFrame of result values with x (unique) as row and y (unique) as column

  27. 27

    Pandas, append column based on unique subset of column values

  28. 28

    Pandas, append column based on unique subset of column values

  29. 29

    Creating Dictionary from Pandas DataFrame Column Based on Unique Values in Column

HotTag

Archive