0

Here is simple of my dataframe

                  A    B     C    D    
Date                                                                         
1                 A1   B1    C1   D1 
2                 A2   B2    C2   D2 
3                 A3   B3    C3   D3 
4                 A4   B4    C4   D4 

so i want to create nest list like [[A1,A2,A3,A4],[B1,B2,B3,B4],....]

i use command like mylist = dataframe.value.tolist()

but it return [[A1,B1,C1,D1],[A2,B2,C2,D2]] instead

so is there a way to get nest list as i want?

#i use python 3.8.5 and pandas dataframe import data from yfinance

3 Answers 3

2

Just transpose and then call values:

df.T.values.tolist()

[['A1', 'A2', 'A3', 'A4'],
 ['B1', 'B2', 'B3', 'B4'],
 ['C1', 'C2', 'C3', 'C4'],
 ['D1', 'D2', 'D3', 'D4']]
Sign up to request clarification or add additional context in comments.

Comments

0

You could loop through columns and append

li = []
for col in dataframe:
    li.append(dataframe[col])

Comments

0

Here is a possible solutions for your problem:

l=[]
for e in df.columns:
    column = df[e].values.tolist()
    l.append(column)

The idea is taking every column as a list, and iterate it for all de columns.

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.