简体   繁体   中英

dataframe how to add a specific row when a string is found in a given column

lets say I have a data frame P and where ever there is an 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)

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM