Home > Mobile >  How to create a Lower and Upper bound column for Pandas Dataframe
How to create a Lower and Upper bound column for Pandas Dataframe

Time:08-02

enter code hereCurrently the pandas dataframe in one column is set up as

Allocation
8100 
8195
8195
8815

Trying to create a lower and upper bound column where it looks like down below. There is thousands of rows for this and I am not sure how to create this at scale using pandas.

Lower Bound   Upper Bound
8100                8195
8195                8815

CodePudding user response:

This should work if you need to alternate rows between upper and lower bound.

(df.set_index(list(divmod(np.arange(df.shape[0]),2)))
.squeeze()
.unstack()
.rename({0:'Lower Bound',1:'Upper Bound'},axis=1))

Output:

   Lower Bound  Upper Bound
0         8100         8195
1         8195         8815

CodePudding user response:

It's not very clear, but it looks like you want your data structured like so:

out = pd.DataFrame(df.to_numpy().reshape(2, len(df)//2), columns=['Lower_Bound', 'Upper_Bound'])

Output:

   Lower_Bound  Upper_Bound
0         8100         8195
1         8195         8815
  • Related