[英]Pandas DF to a nested list
I have a dataframe (df) and I want to transform it to a nested list.我有一个 dataframe (df),我想将它转换为嵌套列表。
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.
嵌套内的第一个列表从第一列的 df 的前 3 行获取值。 The second then the first 3 rows of the second column and the third the 3 first rows of the third column.
第二个是第二列的前 3 行,第三个是第三列的前 3 行。 After that I want to add lists of the remaning 3 rows.
之后,我想添加剩余 3 行的列表。
[[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.我做了一个 for 循环并在每个系列上调用了 tolist() 。 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?
我如何从下面的结果到上面的预期结果 go?
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使用
.values
和一些 numpy 切片
v = df.values.T
v[:,:3].tolist() + v[:,3:].tolist()
output 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()
这是一个从 3 个列表开始的示例,你得到的列表是
.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 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.我认为您只想将列表(列)划分为大小为 n 的列表。 You can change the value of n, to change the sublist size.
您可以更改 n 的值,以更改子列表大小。
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])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.