1

I have a dataframe similar to the following but very large:

col1 col2 0 0 0 0 1 2 3 4 5 6 0 0 In my actual table the numbers are timestamps which are in order, not in one number difference, but one is larger than the previous as shown in this dataframe. I want the two columns to be merged into a single column according the order of numbers. I want the resulting column to be like the following:

column 0 0 1 2 3 4 5 6 0

Thanks in advance.

Same
  • 759
  • 2
  • 9
  • 15
  • I don't quite understand what the structure of the DataFrame you have or the DataFrame you want is. Could you check to see that it's formatted the way you expected? – Josh Rumbut Nov 25 '15 at 23:05
  • I have a dataframe with two columns as shown above. And the entries are timestamps which are in order as shown. Now what I want is, I want to create a column which which is a merging of the two columns according their order. if that makes sense – Same Nov 25 '15 at 23:26
  • That does make sense, give me a minute and I'll come up with the code. – Josh Rumbut Nov 25 '15 at 23:28

1 Answers1

1

Ok here you go, using your contrived DF as an example:

df.column = df.col1.append(df.col2).sort_values()

Upon rereading your question you may be looking for pd.concat

df.column = pd.concat([df.col1,df.col2],axis=0)
Josh Rumbut
  • 2,640
  • 2
  • 32
  • 43
  • I have tried your code but I am getting the following error message: 'Series' object has no attribute 'sort_values' – Same Nov 25 '15 at 23:55
  • Fascinating, can you change it to sort () and see if that helps? – Josh Rumbut Nov 26 '15 at 00:17
  • Oh for some reason sort_values() is working now. but how do I drop all the 0 entries in the resulting column? – Same Nov 26 '15 at 00:26
  • You could try drop_duplicates (), does that do the job? – Josh Rumbut Nov 26 '15 at 00:43
  • Thanks It does do the job. I had another question in another thread which no one seem to give me a proper answer with some codes. here is the link to it [link](http://stackoverflow.com/questions/33898674/python-pandas-resampling). I really need it badly for my research. So if you have time you can try it for me. Thanks a lot – Same Nov 26 '15 at 00:50
  • I am about to turn in for the night. I took a look at the question and it looks doable but I need a little more energy to make the logic look right. Also, I would appreciate any advice on this if you use IPython: http://programmers.stackexchange.com/questions/303501/keeping-an-ipython-project-maintainable-and-reliable – Josh Rumbut Nov 26 '15 at 00:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/96197/discussion-between-same-and-josh-rumbut). – Same Nov 26 '15 at 01:05
  • I am actually new to IPython. I have started using it recently. So I am afraid If I can help you a lot on that. But regarding my question I can explain to you If something is unclear – Same Nov 26 '15 at 01:18