'Sort pandas dataframe in very special order

I want to do a very special order in pandas dataframe.

I have two separated dataframes: dataframe a and dataframe with different sizes. Example: dataframe a size = 10000. dataframe b size = 3000.

I need to append both dataframes and make a special order based on the following: dataframe a batch size= floor(size dataframe a/size dataframe b), in the example: floor(10000/3000) = 3. dataframe b batch size= 1

So, in append dataframe, the order will be

enter image description here

how can I do that in an efficient way ? Thanks in advance



Solution 1:[1]

Use something like:

# pseudo code
index_all = np.arange(len(x1) + len(x2))
ord = floor(…)
index_1 = np.delete(index_all, slice(None, None, ord))
index_2 = index_all[ord::ord]

then merge on index

Edit:

A working example:

import pandas as pd 
import numpy as np
df1 = pd.DataFrame()
df1['values'] = np.random.randint(0, 100, size=10000)

df2 = pd.DataFrame()
df2['values'] = np.random.randint(0, 100, size=3000)

b_size1 = int(np.floor(len(df1)/len(df2)))
index_all = np.arange((len(df1)+len(df2))*2)

index_1 = np.delete(index_all, slice(b_size1, None, b_size1))
index_2 = index_all[b_size1::b_size1]

df1.index = index_1[:len(df1)]
df2.index = index_2[:len(df2)]

pd.concat([df1, df2], axis=0, sort=True)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1