简体   繁体   English

从 Pandas 数据框中获取行数据作为列表

[英]Get row data from a pandas dataframe as a list

I want to get row data as a list in a pandas dataframe.我想将行数据作为 Pandas 数据框中的列表获取。 I can get the data in the correct order(column order) in jupiter notebook but when i run the code as a python file in ubuntu terminal the list is not in order.infact it seemes the list is in assending order.我可以在 jupiter notebook 中以正确的顺序(列顺序)获取数据,但是当我在 ubuntu 终端中将代码作为 python 文件运行时,列表不是按顺序排列的。事实上,列表似乎是按顺序排列的。

this is the data frame这是数据框

Src_mac Dest_mac  Src_Port Dest_port  Byte_Count  duration  Packet_Count
0      02       01         2         1         238  1.000000             3
1      01       02         1         2         140  0.893617             2
2      03       01         2         1         238  0.489362             3
3      01       03         1         2         140  0.446809             2
4      04       01         2         1         238  0.021277             3
5      01       04         1         2         140  0.000000             2

from this code从这个代码

l=list(range(0,len(df.index)))
for i in l:
  d = df.iloc[i]
  ml_data = d.tolist()
  print(ml_data)

The output in Jupyter Notebook is as follows Jupyter Notebook中的输出如下

[2.0, 1.0, 2.0, 1.0, 238.0, 1.0, 3.0]
[1.0, 2.0, 1.0, 2.0, 140.0, 0.8936170212765937, 2.0]
[3.0, 1.0, 2.0, 1.0, 238.0, 0.4893617021276597, 3.0]
[1.0, 3.0, 1.0, 2.0, 140.0, 0.4468085106382951, 2.0]
[4.0, 1.0, 2.0, 1.0, 238.0, 0.02127659574468055, 3.0]
[1.0, 4.0, 1.0, 2.0, 140.0, 0.0, 2.0]

But if i run the same code as a indipendent python file in ubuntu terminal i get this (not in order)但是,如果我在 ubuntu 终端中运行与独立 python 文件相同的代码,我会得到这个(不按顺序)

[238.0, 1.0, 1.0, 3.0, 2.0, 2.0, 1.0]
[140.0, 2.0, 2.0, 2.0, 1.0, 1.0, 0.8936170212765937]
[238.0, 1.0, 1.0, 3.0, 2.0, 3.0, 0.4893617021276597]
[140.0, 3.0, 2.0, 2.0, 1.0, 1.0, 0.4468085106382951]
[238.0, 1.0, 1.0, 3.0, 2.0, 4.0, 0.02127659574468055]
[140.0, 4.0, 2.0, 2.0, 1.0, 1.0, 0.0]

What did i do wrong我做错了什么

If you are using pandas you can consider to use如果您使用的是pandas您可以考虑使用

import pandas as pd
import numpy as np

df = pd.DataFrame(np.arange(9).reshape(3,3))

df.values.tolist()

This returns这返回

[[0, 1, 2], [3, 4, 5], [6, 7, 8]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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