简体   繁体   中英

Get row data from a pandas dataframe as a list

I want to get row data as a list in a pandas dataframe. 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.

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

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

[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

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

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