Skip to content
Advertisement

Taking the 1st and 2nd, 4th and 5th etc rows from a single Pandas column and put in two new columns, Python

Below is a sample of a pandas dataframe, a single column with 1000’s of rows.

I need second/third columns putting data in rows 1 and 2, 4 and 5 etc in the second/third column

JavaScript

Desired Output

JavaScript

Can only manage to pull out the odds with:

JavaScript

Suggestions?

Advertisement

Answer

Make three subsets by taking every third value – starting at 0, 1, and 2. Reset each subset’s index, and concat them all together.

JavaScript

Output:

JavaScript

Slightly more concise:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement