Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Join same name dataframe column values into list

I create a dataframe with distinct column names and use rename to create columns with same name.

import pandas as pd

df = pd.DataFrame({
    "a_1": ["x", "x"],
    "a_2": ["", ""],
    "b_1": ["", ""],
    "a_3": ["", "y"],
    "c_1": ["z", "z"],
})

names = {
    "a_1": "a", 
    "a_2": "a", 
    "a_3": "a", 
    "b_1": "b", 
    "c_1": "c",
}

df2 = df.rename(columns=names)

This produces

   a a b  a  c
0  x         z
1  x      y  z

How to join values in the columns with same name into a list?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

out = pd.DataFrame({
    "a": [["x"], ["x", "y"]],
    "b": [[""], [""]],
    "c": [["z"], ["z"]],
})

        a b    c
0     [x] [] [z]
1  [x, y] [] [z]

Bad attempt

I suspect this can be resolved with lambda

for c in ['a', 'b', 'c']:
    df2[c] = df2[c].apply(lambda x: x.tolist() if x.any() else [], axis=1)
    

This however produces

TypeError: <lambda>() got an unexpected keyword argument 'axis'

Any idea?

>Solution :

You could transpose and groupby.agg with a custom lambda:

df2.T.groupby(level=0).agg(lambda x: x[x!=''].tolist()).T

Output:

        a   b    c
0     [x]  []  [z]
1  [x, y]  []  [z]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading