I have this dataframe:
and I would like to create col_list and add it to the dataframe such that it looks like this:
I have tried this code but it doesn’t work:
JavaScript
x
11
11
1
import pandas as pd
2
import numpy as np
3
4
5
d = {'col1': [0, 2, 4], 'col2': [1, 3, 5], 'col3': [2, 4, 8]}
6
df = pd.DataFrame(d)
7
print(df)
8
9
b = df[['col1', 'col2']].to_numpy()
10
df = pd.DataFrame({'col1': [0, 2, 4], 'col2': [1, 3, 5], 'col3': [2, 4, 8],'col':[np.c_[b]]})
11
Advertisement
Answer
Try this
JavaScript
1
9
1
import pandas as pd
2
import numpy as np
3
4
d = {'col1': [0, 2, 4], 'col2': [1, 3, 5], 'col3': [2, 4, 8]}
5
df = pd.DataFrame(d)
6
7
df["col_list"] = df.values.tolist()
8
df
9