Reputation: 11
lets say I have a data frame P and where ever there is a n in col b,
I want to repeat that row, so that now it looks like:
From:
a b c d
1 v 4 5
4 n 6 7
5 v 6 8
To:
a b c d
1 v 4 5
4 n 6 7
4 n 6 7
5 v 6 8
I am quite new to python and have not found a straight forward way to accomplish this. here is what i have already tried
if P['b']=='v':
P.pd.concat(P.loc,ignore_index=True)
Upvotes: 0
Views: 452
Reputation: 2310
You can use DataFrame.append:
In [1]: df
Out[1]:
a b c d
0 1 v 4 5
1 4 n 6 7
2 5 v 6 8
In [2]: df.append(df.loc[df['b'] == 'n'])
Out[2]:
a b c d
0 1 v 4 5
1 4 n 6 7
2 5 v 6 8
1 4 n 6 7
Note that it appended the row at the end of the DataFrame. If you want to have it next to the row being duplicated, you can use sort_index:
In [3]: df
Out[3]:
a b c d
0 1 v 4 5
1 4 n 6 7
2 5 v 6 8
In [4]: df.append(df.loc[df['b'] == 'n']).sort_index()
Out[4]:
a b c d
0 1 v 4 5
1 4 n 6 7
1 4 n 6 7
2 5 v 6 8
Upvotes: 1
Reputation: 1230
You generally want to avoid looping through the DataFrame whenever you can, so if you want to find all of those rows, using loc with a boolean index can help you find them in one sweep, then you can copy what you found into a separate DataFrame. Then, just concatenate the two.
p_2 = P.loc[P['b']=='n'].copy(deep=True)
P = pd.concat([P,P2],ignore_index=True)
Upvotes: 2