简体   繁体   中英

Pandas DF to a nested list

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]]

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]]

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]]

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]]
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]]

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])

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM