1

I have csv file like below

Id,Date,P_Sales_Qty,Category
1,2016-03-20,1,17
2,2016-03-23,1,17
3,2016-03-25,2,17
4,2016-03-31,2,17
5,2016-04-02,3,17
6,2016-04-04,1,17
7,2016-04-09,1,17
8,2016-04-10,1,17
9,2016-04-28,1,17
10,2016-04-29,2,17
11,2016-04-30,1,17

I want to create a list like :

[2016-03-20 1   17
2016-03-23  1   17
2016-03-25  2   17
2016-03-31  2   17
2016-04-02  3   17
2016-04-04  1   17
2016-04-09  1   17
2016-04-10  1   17
2016-04-28  1   17
2016-04-29  2   17
2016-04-30  1   17]

Please help on this how can I achieve this?

cs95
  • 379,657
  • 97
  • 704
  • 746
Sai
  • 1,075
  • 5
  • 31
  • 58

2 Answers2

1

Just use values property for your dataframe.

list = df.values.tolist()
In[1] : list
Out[1]: [['2016-03-20', 1, 17],
         ..................
         ]

The second step is to use reduce method in order to obtain a simple list.

from functools import reduce
list = reduce(lambda x, y: x+y, list)
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
  • Hi Mihai, thanks for quick reply , I don't want create nested lists.I want create exact output as I mentioned question. – Sai Apr 19 '18 at 13:13
0

It seems need:

a = df[['Date','P_Sales_Qty','Category']].to_csv(sep=' ', header=None, index=False)
print (a)
2016-03-20 1 17
2016-03-23 1 17
2016-03-25 2 17
2016-03-31 2 17
2016-04-02 3 17
2016-04-04 1 17
2016-04-09 1 17
2016-04-10 1 17
2016-04-28 1 17
2016-04-29 2 17
2016-04-30 1 17

Also if wrap it to list get:

a = [df[['Date','P_Sales_Qty','Category']].to_csv(sep=' ', header=None, index=False)]
print (a)

['2016-03-20 1 17\n2016-03-23 1 17\n2016-03-25 2 17\n2016-03-31 2 17\n2016-04-02 3 17\n2016-04-04 1 17\n2016-04-09 1 17\n2016-04-10 1 17\n2016-04-28 1 17\n2016-04-29 2 17\n2016-04-30 1 17\n']
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Thanks for quick reply , I don't want create nested lists.I want create exact output as I mentioned question. – Sai Apr 19 '18 at 13:13
  • @saikumar - Do you think 2d array? Check edited answer. – jezrael Apr 19 '18 at 13:15
  • @jezrael, I think he wants an unwraped list, but its output seems to be strange. – Mihai Alexandru-Ionut Apr 19 '18 at 13:23
  • @jezrael, thanks for reply but i am getting list, for each variable in new line but I want like[ '2016-03-20' 1 17 next line next record like expected output in question – Sai Apr 19 '18 at 13:36
  • @saikumar - I am a bit confused, I modify answer, but still not sure what need. Can you explain more? – jezrael Apr 20 '18 at 06:34
  • @jezrael, sorry for late reply.we are getting output in string type but a list which contains Date is string and reaming columns are int – Sai Apr 20 '18 at 13:03
  • @saikumar - I think what you need is problematic. Maybe little hack help `df.columns = [''] * len(df.columns)` and then `print (df)`. – jezrael Apr 20 '18 at 13:06
  • @saikumar - And `df.index = [''] * len(df.index)` – jezrael Apr 20 '18 at 13:07