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
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']]
You could loop through columns and append
li = []
for col in dataframe:
li.append(dataframe[col])
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.