2

I need to generate a list from pandas DataFrame . I am able to print the result,but i don't know how to convert it to the list format.The code i used to print the result(without converting to list) is

df=pandas.DataFrame(processed_data_format, columns=["file_name", "innings", "over","ball", "individual ball", "runs","batsman", "wicket_status","bowler_name","fielder_name"])**  
  #processed_data_format is the list passing to the DataFrame   
t = df.groupby(['batsman','over'])['runs','ball'].sum()
print t

i am getting the result like

Sangakara 1   10   5

          2    0   2

          3    3   1


    sewag 1    2   1

          2    1   1 

I would like to convert this data into list format like

[ [sangakara,1,10,5],[sangakara,2,0,2],[sangakara,3,3,1],[sewag,1,2,1][sewag,2,1,1] ]

Edwin Baby
  • 279
  • 1
  • 4
  • 11

1 Answers1

1

You can use to_records:

import pandas as pd

df = pd.DataFrame({'a': [1, 2, 3, 4], 'b': [2, 3, 4, 5]})

>>> list(df.to_records())

 [(0, 1, 2), (1, 2, 3), (2, 3, 4), (3, 4, 5)]

This will make a list of tuples. Converting this to a list of lists (if you really need this at all), is easy.

Ami Tavory
  • 74,578
  • 11
  • 141
  • 185