1

I have this dataframe:

enter image description here

and I would like to create col_list and add it to the dataframe such that it looks like this:

enter image description here

I have tried this code but it doesn't work:

import pandas as pd
import numpy as np


d = {'col1': [0, 2, 4], 'col2': [1, 3, 5], 'col3': [2, 4, 8]}
df = pd.DataFrame(d)
print(df)

b = df[['col1', 'col2']].to_numpy()
df = pd.DataFrame({'col1': [0, 2, 4], 'col2': [1, 3, 5], 'col3': [2, 4, 8],'col':[np.c_[b]]})

1 Answer 1

1

Try this

import pandas as pd
import numpy as np

d = {'col1': [0, 2, 4], 'col2': [1, 3, 5], 'col3': [2, 4, 8]}
df = pd.DataFrame(d)

df["col_list"] = df.values.tolist()
df
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.