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?

3
  • Id is column or index ? What return print (df.info()) ? Commented Apr 19, 2018 at 13:08
  • Id is Column not index Commented Apr 19, 2018 at 13:10
  • Thanks, so use my solution. Commented Apr 19, 2018 at 13:13

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Mihai, thanks for quick reply , I don't want create nested lists.I want create exact output as I mentioned question.
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']

8 Comments

Thanks for quick reply , I don't want create nested lists.I want create exact output as I mentioned question.
@saikumar - Do you think 2d array? Check edited answer.
@jezrael, I think he wants an unwraped list, but its output seems to be strange.
@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
@saikumar - I am a bit confused, I modify answer, but still not sure what need. Can you explain more?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.