Sharonio
Sharonio

Reputation: 153

python pandas data-frame - duplicate rows according to a column value

I want to duplicate the rows of dataframe "this" according to 2 column values and save them as a new dataframe named "newThis":

this = pd.DataFrame(columns=['a','b','c'], index=[1,2,3])
this.a = [1, 2, 0]
this.b = [5, 0, 4]
this.c = [2, 3, 2]

newThis = []

for i in range(len(this)):

    if int(this.iloc[i, 1]) != 0:
        that = np.array([this.iloc[i,:]] * int(this.iloc[i, 1]))
    elif int(this.iloc[i, 1]) == 0:
        that = np.array([this.iloc[i,:]])              

    if int(this.iloc[i, 2]) != 0:
        those = np.array([this.iloc[i,:]] * int(this.iloc[i, 2]))
    elif int(this.iloc[i, 2]) == 0:
        those = np.array([this.iloc[i,:]])

    newThis.append(that)
    newThis.append(those)

I want one big array of concatenated rows, but Instead I get this mess:

[array([[1, 5, 2],
        [1, 5, 2],
        [1, 5, 2],
        [1, 5, 2],
        [1, 5, 2]], dtype=int64), array([[1, 5, 2],
        [1, 5, 2]], dtype=int64), array([[2, 0, 3]], dtype=int64), array([[2, 0, 3],
        [2, 0, 3],
        [2, 0, 3]], dtype=int64), array([[0, 4, 2],
        [0, 4, 2],
        [0, 4, 2],
        [0, 4, 2]], dtype=int64), array([[0, 4, 2],
        [0, 4, 2]], dtype=int64)]

Thanks

Upvotes: 2

Views: 1883

Answers (2)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210862

IIUC:

Source DF:

In [213]: this
Out[213]:
   a  b  c
1  1  5  2
2  2  0  3
3  0  4  2

Solution:

In [211]: newThis = pd.DataFrame(np.repeat(this.values, 
                                           this['b'].replace(0,1).tolist(), 
                                           axis=0),
                                 columns=this.columns)

In [212]: newThis
Out[212]:
   a  b  c
0  1  5  2
1  1  5  2
2  1  5  2
3  1  5  2
4  1  5  2
5  2  0  3
6  0  4  2
7  0  4  2
8  0  4  2
9  0  4  2

Upvotes: 3

cwallenpoole
cwallenpoole

Reputation: 82028

It looks like you're confusing multiplying an np.array with a list.

Remember:

 [np.int32(1)] * 2 == [np.int32(1), np.int32(1)]

But:

 np.array([1]) * 2 == np.array([2])

You probably need to change this:

np.array([this.iloc[i,:]] * int(this.iloc[i, 1]))

to this:

np.array([this.iloc[i,:]]) * int(this.iloc[i, 1])

Upvotes: 0

Related Questions