Home > Software design >  Pandas : make new column after nth row
Pandas : make new column after nth row

Time:10-18

I have the following table as a data frame.

0
8
990
15
70
85
36
2
43
5
68
61
62
624
65
82
523
98

I want to create a new column after every third row. So the data should look like this.

enter image description here

Thanks in advance.

CodePudding user response:

import pandas as pd
import numpy as np

numbers= [0,
8,
990,
15,
70,
85,
36,
2,
43,
5,
68,
61,
62,
624,
65,
82,
523,
98]

pd.DataFrame(np.reshape(numbers, (6,3)))

CodePudding user response:

Looks like your column can be converted into an array(i.e., list). If this is the case, you can break down the value by array and create an array of array. Then, use the array of array to create a dataframe.

The code might look something like this:

listofitems = [...]
## create new dataframe based on list index jump
newdf = pd.DataFrame([listofitems[i::3] for i in range(3)])
## transpose dataframe to 3 columns dataframe
newdf = newdf.T

For the example given above, 4139055 rows is not a big data. If you do have a big and complex data, please take a look at PySpark specifically on dataframe with Sparks. This is one of the big data frameworks that help optimizing data transformation over big dataframe.

  • Related