Home > Blockchain >  How to expand dataframe with a calculated list based on rows in python
How to expand dataframe with a calculated list based on rows in python

Time:08-12

I have that kind of data generated by this code

import pandas as pd

def multichoose(n,k):
    if k < 0 or n < 0: return "Error"
    if not k: return [[0]*n]
    if not n: return []
    if n == 1: return [[k]]
    return [[0] val for val in multichoose(n-1,k)]   \
        [[val[0] 1] val[1:] for val in multichoose(n,k-1)]

states=[]
for i in range(0,3):
    states=states multichoose(3,i)
df_states = pd.DataFrame(states,columns=['x1','x2','x3'])
df_states['cumsum']=df_states['x1'] df_states['x2'] df_states['x3']

x1  x2  x3  cumsum
0   0   0   0
0   0   1   1
0   1   0   1
1   0   0   1
0   0   2   2
0   1   1   2
0   2   0   2
1   0   1   2
1   1   0   2
2   0   0   2

And I want to expand my data with this calculation

# For example for the first row
# in range of cumsum value   2
[[x, y] for x in range(df_states[['cumsum']].iloc[0][0] 2) for y in range(df_states[['cumsum']].iloc[0][0] 2)]
    
#output
[[0, 0], [0, 1], [1, 0], [1, 1]]

So my expected result is


x1  x2  x3  cumsum a1 a2
0   0   0   0      0  0
0   0   0   0      0  1
0   0   0   0      1  0
0   0   0   0      1  1
0   0   1   1      0  0
0   0   1   1      0  1
0   0   1   1      0  2
0   0   1   1      1  0
0   0   1   1      1  1
0   0   1   1      1  2
0   0   1   1      2  0
0   0   1   1      2  1
0   0   1   1      2  2
.   .   .   .      .  .
.   .   .   .      .  .

And in the final result, I need to implement this expansion for all rows

Thanks for help <3

CodePudding user response:

Dear trying_to_be_a_dev<

I hope you ared oing well,

import pandas as pd


def multichoose(n, k):
    if k < 0 or n < 0: return "Error"
    if not k: return [[0] * n]
    if not n: return []
    if n == 1: return [[k]]
    return [[0]   val for val in multichoose(n - 1, k)]   \
           [[val[0]   1]   val[1:] for val in multichoose(n, k - 1)]


states = []
for i in range(0, 3):
    states = states   multichoose(3, i)
df_states = pd.DataFrame(states, columns=['x1', 'x2', 'x3'])
df_states['cumsum'] = df_states['x1']   df_states['x2']   df_states['x3']
df_states['a1'] = ''
df_states['a2'] = ''

list1 = []
for i in range(len(df_states)):
    list1.append([[x, y] for x in range(df_states[['cumsum']].iloc[i][0] 2) for y in range(df_states[['cumsum']].iloc[i][0] 2)])

x = []
y = []
for i in range(len(list1)):
    for j in range(len(list1[i])):
        print(list1[i][j][0])
        x.append(list1[i][j][0])
        y.append(list1[i][j][1])

for i in range(len(df_states)):
    df_states['a1'].iloc[i] = x[i]
    df_states['a2'].iloc[i] = y[i]

print(df_states)

enter image description here

For sure this is not the best solution, but it worked for me

Hope it works for you too.

Have a lovely day.

CodePudding user response:

I solved my problem by tweaking AMIR's solution a bit. Thank you so much AMIR.

import pandas as pd

def multichoose(n,k):
    if k < 0 or n < 0: return "Error"
    if not k: return [[0]*n]
    if not n: return []
    if n == 1: return [[k]]
    return [[0] val for val in multichoose(n-1,k)]   \
        [[val[0] 1] val[1:] for val in multichoose(n,k-1)]

states=[]
for i in range(0,3):
    states=states multichoose(3,i)
df_states = pd.DataFrame(states,columns=['x1','x2','x3'])
df_states = df_states.rename_axis('state_num').reset_index()
df_states['cumsum']=df_states['x1'] df_states['x2'] df_states['x3']
df_states

list1 = []
for i in range(len(df_states)):
    list1.append([[x, y] for x in range(df_states[['cumsum']].iloc[i][0] 2) for y in range(df_states[['cumsum']].iloc[i][0] 2)])

df_states['FIFO_LIFO']=list1
df_states.explode('FIFO_LIFO')

    state_num   x1  x2  x3  cumsum  FIFO_LIFO
0   0           0   0   0   0       [0, 0]
0   0           0   0   0   0       [0, 1]
0   0           0   0   0   0       [1, 0]
0   0           0   0   0   0       [1, 1]
1   1           0   0   1   1       [0, 0]
... ...         ... ... ... ...     ...
9   9           2   0   0   2       [2, 3]
9   9           2   0   0   2       [3, 0]
9   9           2   0   0   2       [3, 1]
9   9           2   0   0   2       [3, 2]
9   9           2   0   0   2       [3, 3]
  • Related