Home > OS >  Combine two columns that have lists as values into one column
Combine two columns that have lists as values into one column

Time:04-06

I have the following table :

code value
[1,0] [Yes, No]
[1,2,3] [Yes, No, don't know]

I want to combine the two colmuns into one that would look like this :

Result
1:Yes, 0: No
1: Yes, 2: No, 3: don't know

CodePudding user response:

Use nested list comprehension with join zipped values:

df['Result'] = [', '.join(f'{x}:{y}' for x, y in zip(a, b)) 
                                     for a, b in zip(df.code, df.value)]
print (df)

        code                  value                     Result
0     [1, 0]              [Yes, No]                1:Yes, 0:No
1  [1, 2, 3]  [Yes, No, don't know]  1:Yes, 2:No, 3:don't know

CodePudding user response:

Using apply, but be aware that this is less efficient than an python list comprehension:

df['Result'] = df.apply(lambda r: ', '.join([f'{a}: {b}'
                                             for a,b in zip(r['code'],
                        r['value'])]), axis=1)

output:

        code                  value                        Result
0     [1, 0]              [Yes, No]                 1: Yes, 0: No
1  [1, 2, 3]  [Yes, No, don't know]  1: Yes, 2: No, 3: don't know

reproducible input:

df = pd.DataFrame({'code': [[1, 0], [1, 2, 3]],
                   'value': [['Yes', 'No'], ['Yes', 'No', "don't know"]],
                   'Result': ['1: Yes, 0: No', "1: Yes, 2: No, 3: don't know"]}

                  )

CodePudding user response:

Just like @mozway mentioned, apply is probably the least efficient solution, but the OP added the corresponding tag to the question. Hence, yet another possible solution for the problem in an arguably more pythonic version would be:

df = pd.DataFrame(
    {
        'code': [[1, 0], [1, 2, 3]], 
        'value': [['Yes', 'No'], ['Yes', 'No', 'don\'t know']]
    }
)
df.apply(lambda x: dict(zip(x['code'], x['value'])), axis=1)
 
0                     {1: 'Yes', 0: 'No'}
1    {1: 'Yes', 2: 'No', 3: 'don't know'}

Yet the answer by @jezrael or a similar implementation to that is the better solution.

  • Related