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