[英]Is there a function that creates a list of rows from a python dataframe?
s1 = pd.Series([5, 6, 7])
s2 = pd.Series([7, 8, 9])
df = pd.DataFrame([list(s1), list(s2)], columns = ["A", "B", "C"])
A B C
0 5 6 7
1 8 9 1
and I need : [5,6,7,8,9,1]我需要:[5,6,7,8,9,1]
Is there a function that does this directly?是否有直接执行此操作的函数?
您可以随时将数据帧转换为 numpy 数组并解开:
df.to_numpy().ravel()
Just stack
the dataframe which will create single columnar series, then call tolist()
只需
stack
将创建单个柱状系列的数据tolist()
,然后调用tolist()
>>> df.stack().to_list()
[5, 6, 7, 8, 9, 1]
如果你不想使用 numpy,这里有一个替代方案:
df.T.melt()['value'].to_list()
You can just do你可以做
import numpy as np
np.ravel(df).tolist()
I will assume that your output was typed in incorrectly.我会假设您的输出输入不正确。 Here is my take on the output.
这是我对输出的看法。 Unfortunately, it's not "one function".
不幸的是,它不是“一个功能”。
result = [x for y in df.itertuples(False) for x in y]
Try converting it too a list.尝试将其转换为列表。
import numpy
numpy.ravel(df).tolist()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.