1

I have a dataframe (df) and I want to transform it to a nested list.


df=pd.DataFrame({'Number':[1,2,3,4,5, 6],
             'Name':['A', 'B', 'C', 'D', 'E', 'F'],
             'Value': [223, 124, 434, 514, 821, 110]})

My expected outcome is a nested list. The first list inside the nested takes values from the first 3 rows of df from the first column. The second then the first 3 rows of the second column and the third the 3 first rows of the third column. After that I want to add lists of the remaning 3 rows.

[[1, 2, 3], 
 ['A', 'B', 'C'], 
 [223, 124, 434]
 [4, 5, 6], 
['D', 'E', 'F'],
[514, 821, 110]]

I did a for loop and called tolist() on each series. Then I get all the values from one column in a list. How do I go from the outcome below to the expected outcome above?

col=df.columns

lst=[]
for i in col:
        temp = df[i].tolist()
        temp
        lst.append(temp)

Outcome (lst):

[[1, 2, 3, 4, 5, 6],
 ['A', 'B', 'C', 'D', 'E', 'F'],
 [223, 124, 434, 514, 821, 110]]
1
  • 1
    Something like df.iloc[:3,:].T.values.tolist() + df.iloc[3:,:].T.values.tolist()? Commented Jan 13, 2022 at 11:35

5 Answers 5

3

Use .values and some numpy slicing

v = df.values.T
v[:,:3].tolist() + v[:,3:].tolist()

output

[[1, 2, 3],
 ['A', 'B', 'C'],
 [223, 124, 434],
 [4, 5, 6],
 ['D', 'E', 'F'],
 [514, 821, 110]]
Sign up to request clarification or add additional context in comments.

Comments

2

Try:

lst = df.set_index(df.index // 3).groupby(level=0).agg(list) \
        .to_numpy().ravel().tolist()
print(lst)

# Output
[[1, 2, 3],
 ['A', 'B', 'C'],
 [223, 124, 434],
 [4, 5, 6],
 ['D', 'E', 'F'],
 [514, 821, 110]]

Comments

1

This is an example starting from 3 lists, the ones you got doing .tolist()

a = [1, 2, 3, 4, 5, 6, 4]
b = ['A', 'B', 'C', 'D', 'E', 'F']
c = [223, 124, 434, 514, 821, 110]

res = []

for i in range(len(a) // 3):
  res.append(a[i * 3:(i * 3) + 3])
  res.append(b[i * 3:(i * 3) + 3])
  res.append(c[i * 3:(i * 3) + 3])

result is

[[1, 2, 3], ['A', 'B', 'C'], [223, 124, 434], [4, 5, 6], ['D', 'E', 'F'], [514, 821, 110]]

Comments

1
import pandas as pd
df=pd.DataFrame({
    'Number':[1,2,3,4,5, 6],
    'Name':['A', 'B', 'C', 'D', 'E', 'F'],
    'Value': [223, 124, 434, 514, 821, 110]
})

# convert df into slices of 3

final_list = []
for i in range(0, len(df), 3):
    final_list.append(
        df.iloc[i:i+3]['Number'].to_list())
    final_list.append(
        df.iloc[i:i+3]['Name'].to_list())
    final_list.append(
        df.iloc[i:i+3]['Value'].to_list())
    

print(final_list)

output

[[1, 2, 3], ['A', 'B', 'C'], [223, 124, 434], [4, 5, 6], ['D', 'E', 'F'], [514, 821, 110]]

Comments

1

I think you just want to divide the list (column) into list of size n. You can change the value of n, to change the sublist size.

lst=[]
n=3
for i in col:
        temp = df[i].tolist()
        for i in range(0,len(temp),n):
            lst.append(temp[i:i+n])

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.