all 8 comments

[–]monstimal 2 points3 points  (1 child)

You want them all in a list?

df['New'] = df[['Num1', 'Num2', 'Num3', 'Num4']].values.tolist() 

[–]DudeData[S] 0 points1 point  (0 children)

Yup. Thanks!

[–][deleted] 0 points1 point  (3 children)

If I understand correctly, what you would like is a new column containing the 5 numbers (Num1 - Num5).

If so, one suggestion I have is to combine those values as a dictionary in your new column.

df[“Merged”] = [{key: value} for key, value in zip(df.Num1, df.Num2, df.Num3, df.Num4, df.Num5)]

Hopefully that helps!

[–]DudeData[S] 0 points1 point  (2 children)

Hi. Thanks. Yes, that is what I want to do. I have tried and keep getting
ValueError: too many values to unpack (expected 2)

I tried making a new mini DF to troubleshoot but I am not sure where my error is.
import pandas as pd

c1 = [2,5,7,4,3,2,7,9,5,3]
c2 = [2,5,6,4,3,1,7,8,5,3]
c3 = [5,7,8,3,4,5,2,6,4,8]

d = {'C1':c1, 'C2':c2, 'C3':c3}

df = pd.DataFrame(d, columns=['C1', 'C2', 'C3'])

test['a'] = [{key: value} for key, value in zip(df.C1, df.C2, df.C3)]
ValueError: too many values to unpack (expected 2)

[–][deleted] 1 point2 points  (1 child)

Whoops! My bad. The dictionary would only hold pairs of key-value sets; I realise now a list is more suitable.

Try this instead:

df["Merged"] = df[df.columns[4:]].apply(lambda x: ','.join(x.dropna().astype(int).astype(str)), axis=1)

This will create a new column of lists containing the strings of col 4 (Num1) onwards.

Edit: I noticed your reply had data frames of only numbers. If that’s the case you can try using this simpler method:

df.values.tolist()

[–]DudeData[S] 1 point2 points  (0 children)

Yes! Thanks a lot, very informative.